【发布时间】:2012-07-29 16:21:11
【问题描述】:
使用文本框在给定的 datagridview 列中搜索值,下面的代码将所选行定位在包含输入文本的列上。
private void textBox1_TextChanged(object sender, EventArgs e)
{
//if (Char.IsLetter(e.KeyChar))
//{
for (int i = 0; i < (productDataGridView.Rows.Count); i++)
{
if (productDataGridView.Rows[i].Cells[1].Value.ToString().StartsWith(textBox1.Text, true, CultureInfo.InvariantCulture))
{
productDataGridView.FirstDisplayedCell = productDataGridView[1, i];
productDataGridView.CurrentRow.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
return; // stop looping
}
}
}
问题是在文本框中输入时我无法突出显示或更改所需行的背景颜色,请帮忙?
【问题讨论】:
-
不要相信 FirstDisplayedCell 将行设置为当前行。尝试设置 productDataGridView[1,i].DefaultCellStyle.BackColor... 而不是 CurrentRow?
-
是的,FirstDisplayedCell 没有将行设置为当前行,有没有办法将其设置为当前行?
-
productDataGridView.CurrentRow = ...
-
不起作用,因为它是只读属性。
-
我在打电话,猜的。但是你可以尝试谷歌:'设置当前DataGridView行'。我敢打赌,第一击会给你答案。
标签: winforms c#-4.0 linq-to-sql datagridview