本文介绍了将一组数据行绑定到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码,但datagridview中没有显示。



任何建议?

  string strFilterOption =dtcolnPurchaseProductExpProductNo = 270; 
dgvProductExp.DataSource = dtPurchaseProductExp.Select(strFilterOption);


解决方案

由于使用数组进行数据绑定的默认行为,您无法为DataSource设置属性,Datatable.Select方法将返回的Datarow数组。从 :

所以另一种方式将是创建一个新的DataTable,使用Clone方法从DataTable源获取模式,并填充新数据表与DataRows数组。

  string strFilterOption =dtcolnPurchaseProductExpProductNo = 270; 
DataTable cloneTable;
cloneTable = dtPurchaseProductExp.Clone();
foreach(dtPurchaseProductExp.Select(strFilterOption)中的DataRow行)
{
cloneTable.ImportRow(row);
}
dgvProductExp.DataSource = cloneTable;

或者,您还可以通过BindingSource对象进行绑定,并使用其Filter属性。 >

I have tried the following code, but nothing is displayed in datagridview.

Any Suggestions?

string strFilterOption = "dtcolnPurchaseProductExpProductNo=270";
dgvProductExp.DataSource = dtPurchaseProductExp.Select(strFilterOption);
解决方案

From MSDN

Due to the default behavior for databinding with array, you can't set, for the DataSource property, the array of Datarows that the Datatable.Select method will return. From this post:

So another way would consist to create a new DataTable, using Clone method in order to get the schema from the DataTable source, and populate the new DataTable with the array of DataRows.

string strFilterOption = "dtcolnPurchaseProductExpProductNo=270";
DataTable cloneTable;
cloneTable = dtPurchaseProductExp.Clone();
foreach (DataRow row in dtPurchaseProductExp.Select(strFilterOption))
{
   cloneTable.ImportRow(row);
}
dgvProductExp.DataSource = cloneTable;

Or, you can also do your binding through a BindingSource object and use its Filter property.

这篇关于将一组数据行绑定到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:06