【问题标题】:How to select visible columns in Datagridview bound to DataTable如何在绑定到 DataTable 的 Datagridview 中选择可见列
【发布时间】:2013-05-22 09:51:47
【问题描述】:

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

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 字符串更改为:

"SELECT title,Name,Addrs FROM Customer"

【问题讨论】:

  • 您应该避免使用SELECT * 语法。始终在每个查询中指定您需要的列。
  • 就我而言,有一列不需要显示,但感谢您的建议..

标签: vb.net winforms data-binding datagridview


【解决方案1】:

因此,如果您不想修改查询字符串(正如@Neolisk 注意到的那样,使用Select * 通常是一种不好的做法,但这是另一个争论),因此您获得的列比您想要显示的多:

Solution1(如果数据表中有很多列并且您想显示其中的一些列是理想的)

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

  2. 然后,为要显示的每一列添加一个DatagridiviewColumn
    为了避免必须为 DatagriviewColumn 添加celltemplate(请参阅this),您最好添加一个强类型列(例如DataGridViewTextBoxColumn,以便显示String 值)。为了将列与源绑定,您设置了DataPropertyName 属性,该属性需要与DataTable 中列的ColumnName 匹配。

所以代码是:

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(如果数据表中有很多列,并且您想隐藏其中的一些列,那么您想保留 @ 的好处是理想的987654326@)

  1. AutoGenerateColumns 属性设置为 true(或自 默认为真)

  2. 挂钩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
    

【讨论】:

  • 谢谢克里斯,我会试试你的建议,
【解决方案2】:

如果 gridview autogeneratecolumns 属性设置为 true,请将其更改为 false,然后按照 Raimond 的建议进行操作。示例:

<asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False">
        <Columns>
                <asp:BoundField DataField="title" HeaderText="Title" />
        </Columns>
</asp:GridView>

【讨论】:

  • 这个问题没有提到 ASP。我会假设 WinForms 基于类名DataGridView
【解决方案3】:

我知道这篇文章可以追溯到很久以前,但我在 C# 中遇到了同样的问题,这就是我解决它的方法,效果非常好。

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 - 以您想要的方式过滤 DataView

为此,我使用 DataView 的 RowFilter 属性。 我创建了一个 GetFiltersToString() 方法,该方法将我拥有的每个过滤器控件格式化为与 RowFilter 语法匹配的字符串(有关语法 here 的更多信息,以及 RowFilter here 的 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;
    }

我发现此解决方案允许用户更轻松地更改过滤器。

【讨论】:

    【解决方案4】:

    您必须将列显式添加到网格中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多