簡體   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