选中某行数据
private void btnclearpath_Click(object sender, EventArgs e)
{
//先删除选中行数据
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value = null;
}
//再删除中间空行
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value == null)
{
dataGridView1.Rows.RemoveAt(i);
}
}
}
筛选,建立texbox输入筛选关键词
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8c3437dd9d0868c76933d8b30a28bb1c.jpeg#pic_center)
private void button3_Click(object sender, EventArgs e)
{ //如果数据符合,保留,否则清除
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() != textBox1.Text)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
dataGridView1.Rows[i].Cells[j].Value = null;
}
}
}
//然后删除中间空行
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value == null)
{
dataGridView1.Rows.RemoveAt(i);
}
}
}