从datatable添加特定行到datagridview?

时间:2012-11-09 09:23:45

标签: c# .net datagridview

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

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

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

4 个答案:

答案 0 :(得分:1)

假设您要将行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;

答案 1 :(得分:1)

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

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

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

我希望这会有所帮助。

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

试试这个

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