Results 1 to 5 of 5

Thread: DataGrid doesn't load correctly?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    DataGrid doesn't load correctly?

    I have a WPF app in which I have a DataGrid control that I am trying to bind to a DataTable as the result of a SQL Query. I'm not sure what I'm doing wrong. It seems pretty straight-forward but I guess not. See the screenshot and my code that produces that is below.

    Code:
            private void LoadEmployersToGrid()
            {
                DataTable dt = new DataTable();
    
                Employer emp = new Employer();
    
                cnn = DatabaseConnection();
    
                if (cnn.State == ConnectionState.Open)
                    dt = emp.GetAllEmployerRecords(cnn);
    
                Grid1.ItemsSource = dt.Rows;
            }
    Here is my SQL routine:

    Code:
            public DataTable GetAllEmployerRecords(SqlConnection cnn)
            {
                DataTable dt = new DataTable();
    
                string sqlStr = "SELECT * FROM Employer";
    
                SqlDataAdapter adp = new SqlDataAdapter(sqlStr, cnn);
                adp.SelectCommand = new SqlCommand(sqlStr, cnn);
                adp.Fill(dt);
    
                return dt;
            }
    Thanks,
    Attached Images Attached Images  
    Blake

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: DataGrid doesn't load correctly?

    You're binding to the rows instead of the datatable.

    try
    Code:
    Grid1.ItemsSource = dt;
    instead


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: DataGrid doesn't load correctly?

    That didn't work. See screenshot.
    Attached Images Attached Images  
    Blake

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: DataGrid doesn't load correctly?

    First, you've been here long enough to know that screen shots don't work here. The forum does funky stuff with it. Second, I'm getting up there in the years... I can't read that. Especially the important pat, which is the error. The fact that it is italicized (I think it is) isn't helping either. Even with my second set of eyes on, I can't really see that.


    -tg


    addendum..OK, when I get 4 inches from the screen, I can make it out... apparenty its a differnce between winforms and wpf.. it's looking for a list... I don't remember, and I can't try it, but see if the rows property has a .ToList() method on it...
    Code:
    Grid1.ItemsSource = dt.Rws.ToList();
    If that still doesn't work, I've got nothing. Oddly, I'm having trouble finding the documentation pages for it. *shrug*

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: DataGrid doesn't load correctly?

    I found it...it should be:

    Code:
    Grid1.ItemSource = dt.DefaultView;
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width