[英]How to count rows of datagridview column which has some specific value in c#
[英]Delete rows which has a specific value from DataGridView C#
我有一個WinForm DataGridView,如下所示,其中我需要刪除列中具有特定值的行
在這種情況下,我使用文本字段鍵入Code,然后單擊“刪除”,在列值下具有ITM-000001的所有行。 我不知道從哪里開始。
private void deleteClick(object sender, EventArgs e)
{
String code = txtCode.Text;
// CODE HERE
}
自從我對Windows Forms進行任何操作以來已經有很長時間了(哦,祝一切順利),如果這還差一點,請原諒,但是您應該可以使用類似的方法來完成此操作...
for(int v = 0; v < dataGrid1.Rows.Count; v++)
{
if(string.Equals(dataGrid1[0, v].Value as string, "ITM-000001"))
{
dataGrid1.Rows.RemoveAt(v);
v--; // this just got messy. But you see my point.
}
}
不一定比Matthew的解決方案更好或更簡單; 只是有時我發現有用的另一種方法:
List<DataGridViewRow> RowsToDelete = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
if ( row.Cells[0].Value != null &&
row.Cells[0].Value.ToString() == "TTTT") RowsToDelete.Add(row);
foreach (DataGridViewRow row in RowsToDelete) dataGridView1.Rows.Remove(row);
RowsToDelete.Clear();
這樣,您可以將列表用作之后的緩沖區,或者在實際刪除之前提供命中數。
我也喜歡他對string.Equals
使用,在這里只是為了好玩而有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.