I have a database with a few tables running on SQL Server 2008, and I’m programming a Windows app w/ .NET 3.5, using the classes provided to query the server. I’m having an issue with a seemingly persistent “blank” row being included in every table.
For example, I bind a DataGridView to a BindingSource, who’s DataSource is a table constructed by a SqlDataAdapter that connects to my database, and I display the results on my DataGrid. The table has 1 record in it:
dataGridView->DataSource = bindingSource;
bindingSource->DataSource = GetData(
"Select * From xxxx", Get_DB_String(), dataAdapter );
Where GetData is defined as:
DataTable^ GetData(
String^ sqlCommand,
String^ connectionString,
SqlDataAdapter^ adapter )
{
SqlConnection^ Connection = gcnew SqlConnection( connectionString );
SqlCommand^ command = gcnew SqlCommand( sqlCommand,Connection );
adapter->SelectCommand = command;
DataTable^ table = gcnew DataTable;
adapter->Fill(table);
return table;
}
And the following grid is what it produces:

If I check the table->Rows->Count, right after the adapter fills the table, it produces a 1, as does the bindingSource->Count. However, the DataGridView reports 2 rows when checking rows->Count.
Now, if I load the SQL Server Management Studio and look at my table, I’m presented with the following view:

However, the row is not able to be deleted/removed, and if I edit any of the contents of a cell in a row it simply adds a new entire NULL row. I assume this is by design for some purpose I’m not aware of, but I’m also hung up on this being the issue.
If I try to explicitly delete the row from the DataGridView, it returns a: “Uncommitted new row cannot be deleted”, and I can’t try to remove the row from my table after filling it with the SqlDataAdapter because it doesn’t report it as being there.
This problem is wreaking havoc on not only the aesthetics of the app, but also the functionality, because I can’t run routines on a bound DataGridView with data that doesn’t exist.
I hope I was being concise enough to convey my problem. Thank you.
This row is for inserting new records in your dataset. If you leave it blank then it just stays blank. If you start editing it – it waits for the edit to finish (when you set focus out of this row) and adds a new record with specified field values.
If you want to disable this functionality in DataGridView then just set the property
AllowUserToAddRowsto false, like this: