如何在绑定到 DataTable 的 Datagridview 中选择可见列 [英] How to select visible columns in Datagridview bound to DataTable

查看:22
本文介绍了如何在绑定到 DataTable 的 Datagridview 中选择可见列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Customer DataTable 上有这些字段:ID,title,Name,Addrs,email,Fax 和此代码绑定 <代码>DataGridView:

I have these fields on Customer DataTable: ID,title,Name,Addrs,email,Fax and this code to bind the DataGridView:

Dim sql As String = "SELECT * FROM Customers"
Dim daHeader As New SqlDataAdapter(sql, Conn)
daHeader.Fill(dsNota, "Customers")
dgvHeader.DataSource = dsNota.Tables("Customers")

如何在DataGridView中查看title,name,addrs数据将SQL字符串更改为:

How do I view title,name,addrs data in DataGridView without changing the SQL string to:

"SELECT title,Name,Addrs FROM Customer"

推荐答案

所以如果你不想修改你的查询字符串(正如@Neolisk 注意到这通常是使用 Select * 但这是另一个争论),所以你得到的列比你想显示的要多:

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:

解决方案 1(如果数据表中有很多列并且您想显示其中的一部分,则是理想的选择)

Solution1 (Ideal if there are a lot of columns in datatable and you want to display just some of them)

  1. 您需要设置 AutoGenerateColumns 属性为假.默认为 True,因此 DataGridView 将为数据表中的所有列创建一列.

  1. You need to set AutoGenerateColumns property to false. Default is True, so DataGridView will create a column for all columns in the datatable.

然后,为要显示的每一列添加一个 DatagridiviewColumn.
为了避免必须添加 celltemplate 对于 DatagriviewColumn(参见 this)你更愿意添加强类型列(DataGridViewTextBoxColumn 例如为了显示 String 值).为了将列与源绑定,您设置 DataPropertyName 属性,需要与DataTable中列的ColumnName匹配.

Then, you add a DatagridiviewColumn for each column you want to display.
To avoid to have to add a celltemplate for the DatagriviewColumn (see this) you would prefer to add a strongly typed column (DataGridViewTextBoxColumn for example in order to display String values). In order to bind the column with the source, you set the DataPropertyName property, that needs to match with the ColumnName of the column inDataTable.

所以代码是:

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")

<小时>

解决方案 2(如果数据表中有很多列,并且您想隐藏其中的一些列,因此您希望保持 隐藏a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autogeneratecolumns%28v=vs.80%29.aspx?cs-save-lang=1&cs-lang=vb" rel="nofollow noreferrer">AutoGenerateColumns)


Solution2 (Ideal if there are a lot of columns in datatable, and you want to Hide just some of them, and so you want to keep the benefit of AutoGenerateColumns)

  1. 设置 AutoGenerateColumns 属性设置为真(或者因为默认为真)

  1. Set AutoGenerateColumns property to true (or do nothing since default is True)

挂钩 DataGridView.DataBindingComplete 事件以隐藏一些自动生成的列:

Hook the DataGridView.DataBindingComplete Event 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

这篇关于如何在绑定到 DataTable 的 Datagridview 中选择可见列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆