繁体   English   中英

从datatable添加特定行到datagridview?

[英]adding specific rows from datatable to datagridview?

我有一个包含1000行的数据表,现在我想在第一个索引和最后一个索引之间添加一系列行,从这个数据表到我的数据网格视图。 我该如何做到这一点?

数据表还包含有关我总是希望在datagridview中维护的列的信息。

PS:First Index和Last Index是一些整数变量。 这是在c#中使用.net平台。

假设您要使用行100到200作为DataSource,您可以使用Enumerable.Skip/Take

datagridView1.DataSource = table.AsEnumerable()
                                .Skip(100)
                                .Take(100)
                                .CopyToDatatable();

从startIndex到带有Enumerable.Where endIndex:

datagridView1.DataSource =  table.AsEnumerable()
                                 .Where((r, i) => i >= startIndex && i <= endIndex)
                                 .CopyToDatatable();

记得using System.Linq;添加using System.Linq;

您可以对数据使用过滤器来限制DataGridView显示的结果。 做一些事情

DataTable tmpDt = GetDataTable();
BindingSource source2 = new BindingSource();
source2.DataSource = tmpDt;
source2.Filter = "columnValue < 100 AND columnValue > 200";
dataGridView2.DataSource = source2;

这种方法的优点是过滤器不会破坏您的基础数据。 您可以使用Filter更新DataGridView显示的数据。

我希望这有帮助。

谷歌搜索给我准备使用的文章,

如何:将数据绑定到Windows窗体DataGridView控件 - http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database

试试这样吧

DataRow[] rw = myDataTable.Select("#" + firstindex+ "# >= FirstIndexCol 
                                     AND SecondIndexCol <= #" + SecondIndex+ "#");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM