vb.net 如何在绑定到 DataTable 的 Datagridview 中选择可见列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16688760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select visible columns in Datagridview bound to DataTable
提问by Lucky
I have these fields on CustomerDataTable: ID,title,Name,Addrs,email,Faxand this code to bind the DataGridView:
我在Customer上有这些字段DataTable:ID、title、Name、Addrs、email、Fax和这个代码来绑定DataGridView:
Dim sql As String = "SELECT * FROM Customers"
Dim daHeader As New SqlDataAdapter(sql, Conn)
daHeader.Fill(dsNota, "Customers")
dgvHeader.DataSource = dsNota.Tables("Customers")
How do I view title,name,addrsdata in DataGridViewwithoutchanging the SQL string to:
如何在不将 SQL 字符串更改为以下内容的情况下查看标题、名称、地址数据 :DataGridView
"SELECT title,Name,Addrs FROM Customer"
采纳答案by Chris
So if you don't want to modify your query string (as @Neolisk noticed this is generally a bad practice to use Select *but this is another debat), and so you get more columns than what you want to display:
因此,如果您不想修改查询字符串(正如@Neolisk 注意到的那样,这通常是一种不好的做法,Select *但这是另一个争论),因此您将获得比想要显示的更多的列:
Solution1(Ideal if there are a lot of columns in datatable and you want to displayjust some of them)
解决方案1(如果数据表中有很多列并且您只想显示其中的一些列,则是理想的选择)
You need to set AutoGenerateColumnsproperty to false. Default is True, so DataGridView will create a column for all columns in the datatable.
Then, you add a
DatagridiviewColumnfor each column you want to display.
To avoid to have to add a celltemplatefor the DatagriviewColumn (see this) you would prefer to add a strongly typed column (DataGridViewTextBoxColumnfor example in order to displayStringvalues). In order to bind the column with the source, you set the DataPropertyNameproperty, that needs to match with theColumnNameof the column inDataTable.
您需要将AutoGenerateColumns属性设置为 false。默认为 True,因此 DataGridView 将为数据表中的所有列创建一列。
然后,
DatagridiviewColumn为要显示的每一列添加一个。
为避免必须为DatagriviewColumn添加单元格模板(请参阅此),您更愿意添加强类型列(例如DataGridViewTextBoxColumn以显示String值)。为了与源列绑定,可以设置DataPropertyName属性时,需要以配合ColumnName在列DataTable。
So code would be:
所以代码将是:
dgvheader.AutoGenerateColumns = False
dgvHeader.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Title", .DataPropertyName = "title"})
dgvHeader.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Name", .DataPropertyName = "Name"})
dgvHeader.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Adresse", .DataPropertyName = "Addrs"})
dgvHeader.DataSource = dsNota.Tables("Customers")
Solution2(Ideal if there are a lot of columns in datatable, and you want to Hidejust some of them, and so you want to keep the benefit of AutoGenerateColumns)
解决方案2(如果数据表中有很多列,并且您只想隐藏其中的一些列,因此您想保留AutoGenerateColumns的好处,则是理想的选择)
Set AutoGenerateColumnsproperty to true (or do nothing since default is True)
Hook the DataGridView.DataBindingCompleteEvent in order to hide some columns autogenerated:
Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As DataGridViewBindingCompleteEventArgs) Handles dgvHeader.DataBindingComplete With dgvHeader .Columns("Fax").Visible = False End With End Sub
将AutoGenerateColumns属性设置为 true(或什么都不做,因为默认值为 True)
挂钩DataGridView.DataBindingComplete事件以隐藏一些自动生成的列:
Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As DataGridViewBindingCompleteEventArgs) Handles dgvHeader.DataBindingComplete With dgvHeader .Columns("Fax").Visible = False End With End Sub
回答by Moon Rat
I know this post goes way back, but I had the same problem in C# and this is how I solved it which works really well.
我知道这篇文章可以追溯到很久以前,但我在 C# 中遇到了同样的问题,这就是我解决它的方法,效果非常好。
1 - Build a DataSet with your SQL query
1 - 使用 SQL 查询构建数据集
private void LoadDS()
{
// this method gets data from my database
// DS is a DataSet in the properties of my form
DS = LoadData();
}
2 - Get the DataView filtered the way you want
2 - 以您想要的方式过滤 DataView
For this I use the RowFilterproperty of the DataView. I created a GetFiltersToString() method that formats every filter control I have in a string that matches the RowFilter syntax (more information about the syntax here, and msdn definition of RowFilter here)
为此,我使用了 DataView的RowFilter属性。我创建了一个 GetFiltersToString() 方法,该方法将我在匹配 RowFilter 语法的字符串中的每个过滤器控件格式化(有关此处语法的更多信息,以及此处RowFilter 的 msdn 定义)
public void RefreshDGV()
{
// Get the corresponding dataview
DV = new DataView(DS.Tables[0], rowFilter, "SORTINGCOLUMN Desc", DataViewRowState.CurrentRows);
// Display it in a datagridview
DGV.DataSource = DV;
}
I find that this solution allows user to change the filtering much more easily.
我发现此解决方案允许用户更轻松地更改过滤。
回答by Raimond Kuipers
You have to explicitly add the columns to your grid.
您必须明确地将列添加到您的网格中。
回答by Ashley Webb
If the gridview autogeneratecolumns attribute is set to true change it to false and then do as Raimond suggested. Example:
如果 gridview autogeneratecolumns 属性设置为 true,请将其更改为 false,然后按照 Raimond 的建议进行操作。例子:
<asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" />
</Columns>
</asp:GridView>

