Thread: DataTable binding....

  1. #1
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003

    DataTable binding....

    Here is the method I created. I am trying to read a table from a database and put that information within a DataTable which i have no issues doing. Then I want to bind that DataTable to a DataGridView object I have on my form. I have no problem doing that but its the formatting I am having issues with. Ive tried using the Wizard for binding a database and pulling the columns but it screws things up with I try to remove or update the database using the bounded DataGridView.

    What I want is to get all the columns in the DataTable, bind that to the DataGridView (So I can access the ID -first- column easy) but only show 3 out of the 5 columns in the table on my datagridview object. I have already created the datagridview, set the three columns widths, and changed the HeaderText so it looks nice instead of the column titles the database gives.

    Basically what I get is the thee very nice looking colums which the data like I want, then the whole DataTable gets added to the right of those three columns. So How do I Bind the DataTable and hide those other columns from appearing????

    If i comes down to changn my query string to just get those three columns and the ID column(first column) then I will but I would still want to hide the ID column so the user can not see it.

          private void UpdatePlayerRounds()
             DataTable playerRoundsTable = new DataTable();
             DataRow roundRow;
             // Set the query command string.
             cmdString = "Select * From roundOfGolfTable " +
                         "WHERE [playerName] = '" + 
                         golferNameComboBox.Text + "'";
             dbCommand.CommandText = cmdString;
             dbCommand.Connection = dbConn;
             // Execute the Command string.
             dbReader = dbCommand.ExecuteReader();
             // Read each column name and add to the DataTable.
             for (int tmp = 0; tmp < dbReader.FieldCount; tmp++)
             // Read each row from the query and add each row to the DataTable.
             while (dbReader.Read())
                roundRow = playerRoundsTable.NewRow();
                for (int tmp2 = 0; tmp2 < dbReader.FieldCount; tmp2++)
                   roundRow[dbReader.GetName(tmp2)] = dbReader.GetValue(tmp2);
             // Associate the datagridview "golfRoundDG" with the DataTable.        
             golfRoundDG.DataSource = playerRoundsTable;
             scoreCol.DataPropertyName = "roundScore";
             courseCol.DataPropertyName = "courseName";
             dateCol.DataPropertyName = "roundDate";
    The keyboard is the standard device used to cause computer errors!

  2. #2
    Registered User
    Join Date
    Jan 2008
    Set the AutoGenerateColumns property to false.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Perfect! Thank you very much!
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Late or early binding?
    By Sharke in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2009, 12:28 PM
  2. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  3. Exporting a DataTable to Excel (.NET)
    By Trauts in forum C# Programming
    Replies: 0
    Last Post: 07-29-2005, 01:03 PM
  4. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM