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();
// 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.
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";
Set the AutoGenerateColumns property to false.
Perfect! Thank you very much!