简体   繁体   中英

Unwanted name column in DataGridView - How can I get rid of it?

I'm making a WinForms application using VB.NET. I have a DataGridView that I am using to display results of a query to an SQL-Server Database. This works fine:

Dim SQL As New SqlConnection()
Dim CMD As New SqlCommand()
Dim ADP As New SqlDataAdapter()
Dim TBL As New DataTable()

SQL.Close()
SQL.ConnectionString = "Server=" & GetServerIP() & ";Database=" & GetDB() & ";User Id=" & userID & ";Password=" & pass & ";"
CMD.Connection = SQL
CMD.CommandText = "SELECT * FROM " & table

SQL.Open()

ADP.SelectCommand = CMD
TBL.Clear()
ADP.Fill(TBL)

DataGridView1.DataSource = TBL

The issue is that the DataGridView has a column NAME which is empty in every row. 看起来像这样

I tried changing the query to SELECT SEQ_NUM FROM CONFIG_SYS that resulted in a DataGridView with a NAME and SEQ_NUM column. I tried calling DataGridView1.Columns.Clear() before the DataGridView1.DataSource = TBL and that does not have an effect on anything. I should mention that if I query any table in my database that does not have a NAME column, I get the same results with an empty NAME column in my DataGridView.

My theory is that a DataGridView automatically adds NAME as the first column, expecting the Database Table to be set up with a NAME column as the primary key. However, my table does not have NAME, it has a column called SEQ_NUM as the primary key. I am able to work around this by doing DataGridView1.Columns("NAME").Visible = False but I don't feel that this is the best way to do it.

Is there a better way to handle this - I'd like the column to not exist at all in the data grid, not just not be visible.

What was causing my issue was the fact that I was calling TBL.Clear() incorrectly.

Calling DataTable.Clear() will clear the Data from a DataTable but it will leave the columns in place.

Instead I should have been calling TBL.Columns.Clear() , which gets rid of the columns of the DataTable. However, using Breakpoints and Visual Studio's DataTable Visualizer I noticed that only calling TBL.Columns.Clear() left the empty rows still in the DataTable. Therefore, to answer the question of how to get rid of a column in a DataGridView that you don't know why/how is there, I suggest to call:

DataTable.Clear()
DataTable.Columns.Clear()

Before setting your DataGridView equal to your DataTable.

Taken from the MSDN:

The .Clear() Method of a DataTable Class "clears the DataTable of all data."

The DataTable.Columns Property gets "a DataColumnCollection that contains the collection of DataColumn objects for the table"

The DataColumnCollection Class also has a .Clear() Method which "clears the collection of any columns."


Thank you @TnTinMn for pointing me in the right direction by using breakpoints and @LarsTech for making me realize that I harshly mixed up DataGridView and DataTable in my original answer.

I hope this post can help others prevent themselves from ripping their hair out about where a non-existent column in their databases seemingly materialized from... If this does not fix your specific issue. I would also check the Query, Database Table setup, and the DataGridView; but for me the issue was with the DataTable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM