[英]how to compare two dgv and pass datagridview to another Form c#
[英]How to compare two datagridViewRows and pass it to 3rd datagridView c#
我有3個datagridViews。 2個datagridViews具有列[“ ID”]和[“ Name”]
`datagridView1
| ID | Name |
1 Juan
2
datagridView2
| ID | Name |
1 Juan`
現在我的問題是,如果datagridView2中確實存在datagridView1行,則datagridView2會將行傳遞給datagridView3,如果datagridView2中不存在datagridView1行,則datagridView1將行傳遞至datagridView3
您可以轉到ID列(datagridView1.Rows [row] .Cells [“ ID”]。Value),也可以使用json解析器(例如Newtonsoft),並比較兩個json字符串以進行完全驗證“名稱”字段。
問候,未編碼
我會遵循以下思路:
// All the grids have the same structure
// dataGridView1 columns: ID1, Name1
// dataGridView2 columns: ID2, Name2
// dataGridView3 columns: ID3, Name3
// Get the unique identificators from dataGridView2
var grid2IDs = (from r in dataGridView2.Rows.Cast<DataGridViewRow>() where r.Cells["ID2"].Value != null select (int)r.Cells["ID2"].Value).Distinct();
// loop trough rows from dataGridView1 where ID is within grid2 unique IDs
foreach (DataGridViewRow row in from r in dataGridView1.Rows.Cast<DataGridViewRow>() where r.Cells["ID1"].Value != null && grid2IDs.Contains((int)r.Cells["ID1"].Value) select r)
{
// Insert the row into dataGirdView3
var newRow = dataGridView3.Rows[dataGridView3.Rows.Add()];
for (var i = 0; i < row.Cells.Count; i++)
newRow.Cells[i].Value = row.Cells[i].Value;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.