【问题标题】:Updating DataSource for DataGridView when DataSource set in properties在属性中设置 DataSource 时更新 DataGridView 的 DataSource
【发布时间】:2018-10-03 23:16:08
【问题描述】:

我有一个DataGridView 并在DataGridView 属性中设置了DataSource(数据集DsAll)和列。

现在我想更新源/数据集。也就是说,我希望能够限制或扩展网格中显示的数据记录,方法是在 TextBox 中输入一个值并仅搜索该值在特定字段中的那些记录(例如员工 ID 或部门号码等...)

当我手动设置数据源/绑定时,我得到了这个工作,但我在使用属性设置数据源时遇到了问题。

任何帮助表示赞赏。

【问题讨论】:

  • 查看行过滤器。编辑:见这里:stackoverflow.com/questions/5843537/…
  • 您应该将DataTable 绑定到BindingSource,然后将其绑定到网格。然后,您将设置该BindingSourceFilter 属性。实际的过滤器字符串的格式与DataTableDefaultView.RowFilter 一样,但这是一种更简洁的方式。

标签: vb.net datagridview dataset datasource


【解决方案1】:

为了详细说明我的评论,我建议在设计器中添加BindingSource,然后绑定如下内容:

myDataAdapter.Fill(myDataTable)
myBindingSource.DataSource = myDataTable
myDataGridView.DataSource = myBindingSource

然后过滤类似这样的内容:

myBindingSource.Filter = String.Format("MyColumn LIKE '%{0}%'", myTextBox.Text)

【讨论】:

    猜你喜欢
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多