简体   繁体   English

DataGridView中不需要的名称列 - 如何摆脱它?

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

I'm making a WinForms application using VB.NET. 我正在使用VB.NET制作WinForms应用程序。 I have a DataGridView that I am using to display results of a query to an SQL-Server Database. 我有一个DataGridView,用于向SQL-Server数据库显示查询结果。 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. 问题是DataGridView有一个NAME列,每行都是空的。 看起来像这样

I tried changing the query to SELECT SEQ_NUM FROM CONFIG_SYS that resulted in a DataGridView with a NAME and SEQ_NUM column. 我尝试将查询更改为SELECT SEQ_NUM FROM CONFIG_SYS ,这导致带有NAME和SEQ_NUM列的DataGridView。 I tried calling DataGridView1.Columns.Clear() before the DataGridView1.DataSource = TBL and that does not have an effect on anything. 我尝试在DataGridView1.DataSource = TBL之前调用DataGridView1.Columns.Clear() ,这对任何事情都没有影响。 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. 我应该提一下,如果我查询数据库中没有NAME列的任何表,我在DataGridView中使用空NAME列获得相同的结果。

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. 我的理论是,DataGridView会自动添加NAME作为第一列,期望使用NAME列作为主键来设置数据库表。 However, my table does not have NAME, it has a column called SEQ_NUM as the primary key. 但是,我的表没有NAME,它有一个名为SEQ_NUM的列作为主键。 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. 我可以通过执行DataGridView1.Columns("NAME").Visible = False解决这个问题DataGridView1.Columns("NAME").Visible = False但我不认为这是最好的方法。

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. 导致我的问题的原因是我错误地调用了TBL.Clear()

Calling DataTable.Clear() will clear the Data from a DataTable but it will leave the columns in place. 调用DataTable.Clear()将清除DataTable.Clear()的数据,但它会保留列。

Instead I should have been calling TBL.Columns.Clear() , which gets rid of the columns of the DataTable. 相反,我应该调用TBL.Columns.Clear() ,它删除了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. 但是,使用Breakpoints和Visual Studio的DataTable Visualizer,我注意到只有调用TBL.Columns.Clear()才会在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: 因此,为了回答如何摆脱DataGridView中的一个你不知道为什么/如何存在的列的问题,我建议打电话:

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

Before setting your DataGridView equal to your DataTable. 在将DataGridView设置为等于DataTable之前。

Taken from the MSDN: 摘自MSDN:

The .Clear() Method of a DataTable Class "clears the DataTable of all data." DataTable类.Clear()方法 “清除所有数据的DataTable”。

The DataTable.Columns Property gets "a DataColumnCollection that contains the collection of DataColumn objects for the table" DataTable.Columns属性获取“包含表的DataColumn对象集合的DataColumnCollection”

The DataColumnCollection Class also has a .Clear() Method which "clears the collection of any columns." DataColumnCollection类还有一个.Clear()方法,它“清除任何列的集合”。


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. 感谢@TnTinMn通过使用断点和@LarsTech指出我正确的方向让我意识到我在原始答案中严重混淆了DataGridView和DataTable。

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; 我还会检查Query,Database Table设置和DataGridView; but for me the issue was with the DataTable. 但对我来说,问题出在DataTable上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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