Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Databinding a DataGridView not immediately available

Databinding a DataGridView not immediately available

Scheduled Pinned Locked Moved C#
databasewpfwcfquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    I have a form containing a DataGridView which I want to populate immediately upon creating the new form. The DataGridView is populated from a SQL query and I do it as follows:

    using (SqlCommand mySqlCommand = new SqlCommand("SELECT * FROM MyTable", mySqlConnection))
    {
    using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
    {
    DataSet myDataSet = new DataSet();
    DataTable myDataTable = new DataTable();
    myDataSet.Tables.Add(myDataTable);
    myDataSet.Load(mySqlDataReader, LoadOption.PreserveChanges, myDataSet.Tables[0]);
    myDataGridView.DataSource = myDataSet.Tables[0];
    }
    }

    myDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
    foreach(DataGridViewRow myRow in myDataGridView.Rows)
    // Do something with some of the values in the row

    Now, the above code works perfectly except for the last two lines of code, which automatically resizes each column to fit the widest cell and which iterates through the DataGridView and does something with some of the values. The reason this won't work is because, by the time they are executed, the databinding for the DataGridView might not yet be finished. One solution is to add

    myDataGridView.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(myDataGridView_DataBindingComplete);

    And then put those lines of code inside the event handler. For various reasons, it happens from time to time that I'd rather have those lines of code inside the original function that populates the form. I'd be quite happy to enter a loop that will wait for the data binding to complete before I continue but I don't know how. Any suggestions?

    J 1 Reply Last reply
    0
    • D Dewald

      I have a form containing a DataGridView which I want to populate immediately upon creating the new form. The DataGridView is populated from a SQL query and I do it as follows:

      using (SqlCommand mySqlCommand = new SqlCommand("SELECT * FROM MyTable", mySqlConnection))
      {
      using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
      {
      DataSet myDataSet = new DataSet();
      DataTable myDataTable = new DataTable();
      myDataSet.Tables.Add(myDataTable);
      myDataSet.Load(mySqlDataReader, LoadOption.PreserveChanges, myDataSet.Tables[0]);
      myDataGridView.DataSource = myDataSet.Tables[0];
      }
      }

      myDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
      foreach(DataGridViewRow myRow in myDataGridView.Rows)
      // Do something with some of the values in the row

      Now, the above code works perfectly except for the last two lines of code, which automatically resizes each column to fit the widest cell and which iterates through the DataGridView and does something with some of the values. The reason this won't work is because, by the time they are executed, the databinding for the DataGridView might not yet be finished. One solution is to add

      myDataGridView.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(myDataGridView_DataBindingComplete);

      And then put those lines of code inside the event handler. For various reasons, it happens from time to time that I'd rather have those lines of code inside the original function that populates the form. I'd be quite happy to enter a loop that will wait for the data binding to complete before I continue but I don't know how. Any suggestions?

      J Offline
      J Offline
      Jasmine2501
      wrote on last edited by
      #2

      Can't you just call the DataBind function explicitly? like... myDataGridView.Databind(); Everything should be ready to go after that...

      D 1 Reply Last reply
      0
      • J Jasmine2501

        Can't you just call the DataBind function explicitly? like... myDataGridView.Databind(); Everything should be ready to go after that...

        D Offline
        D Offline
        Dewald
        wrote on last edited by
        #3

        No, I believe that works only for Webforms, not for Winforms as is the case here. I don't understand why that method is not available in winforms as it would have been the perfect solution for this problem of mine.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups