我将查询的结果转储到datagridview中。它看起来像这样:
QC3 498.46
QC4 1,251.63
QC1 862.62
QC2 1,432.21
我需要两件事
862.62
的数据网格的所有行(或只删除第三行)QC4
,第二个字段为1,251.63
答案 0 :(得分:5)
能够以编程方式删除第二个字段= 862.62
的所有datagrid行
var rowsToRemove = from DataGridViewRow r in dataGridView1.Rows
where r.Cells[1].Value.ToString() == "862.62" // use whatever conversion is appropriate here
select r;
foreach (var r in rowsToRemove)
dataGridView1.Rows.Remove(r);
要删除特定索引处的行,请致电RemoveAt
:
dataGridView1.Rows.RemoveAt(2);
我需要以编程方式突出显示并向下滚动到该行以向用户显示第一个字段为QC4,第二个字段为1,251.63
找到要选择的行,然后设置Selected
属性和FirstDisplayedScrollingRowIndex
属性:
rowToSelect.Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = rowToSelect.Index;
答案 1 :(得分:1)
这是一个非常基本的例子 - 它需要一些工作,但我认为这是正确的方向。
private void Form1_Load(object sender, EventArgs e)
{
List<TestClass> list = new List<TestClass>();
list.Add(new TestClass() { Prop1="QC1",Prop2="1.000"});
list.Add(new TestClass() { Prop1 = "QC2", Prop2 = "2.000" });
list.Add(new TestClass() { Prop1 = "QC3", Prop2 = "3.000" });
list.Add(new TestClass() { Prop1 = "QC4", Prop2 = "4.000" });
dataGridView1.DataSource = list;
}
public class TestClass
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public TestClass()
{
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value == "QC3" && row.Cells[1].Value == "3.000")
row.Selected = true;
}
}
我希望这会有所帮助:)