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

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

问题描述

我在客户 DataTable ID,标题,名称,地址,电子邮件,传真和这个代码绑定 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")

如何查看<$ c $中的 title,name,addrs 数据c> DataGridView 没有将SQL字符串更改为:

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

"SELECT title,Name,Addrs FROM Customer"


推荐答案

不想修改你的查询字符串(因为@Neolisk注意到这通常是一个糟糕的做法,使用选择* ,但这是另一个辩论),所以你得到更多的列比您想显示的内容

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:

Solution1 (如果datatable和y中有很多列,则为理想状态你想要显示只是其中一些)

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


  1. 你需要设置 AutoGenerateColumns 属性为false。
    默认值为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 (请参阅 this ),你更喜欢添加一个强类型的列( DataGridViewTextBoxColumn ,例如为了显示 String values)。为了将列与源绑定,您可以设置 DataPropertyName 属性,需要匹配 DataTable 中的列的 ColumnName code>。

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






Solution2 (如果datatable中有很多列,那么您想要隐藏只是其中一些,所以你想要保持 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. a href =http:// ms dn.microsoft.com/en-us/library/system.windows.forms.datagridview.autogeneratecolumns%28v=vs.80%29.aspx?cs-save-lang=1&cs-lang=vbrel =nofollow > AutoGenerateColumns 属性为true(或从
    默认为True起为无效)

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

挂接 DataGridView.DataBindingComplete 事件为了隐藏一些列自动生成: p>

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


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

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