【问题标题】:How to highlight a datagridview row when a character is typed in the search textbox在搜索文本框中键入字符时如何突出显示 datagridview 行
【发布时间】: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


【解决方案1】:

试试这个

      //restore backcolor of rows to default e.g. loop through grid and set backcolor to white
       foreach(DataGridViewRow row in productDataGridView.Rows)
        {
           if (row.Cells[1].Value.ToString().StartsWith(textBox1.Text, true,     CultureInfo.InvariantCulture))
            {
                //productDataGridView.FirstDisplayedCell = productDataGridView[1, i];
                row.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                return; // stop looping 
            }
            else
              //Set backcolor to default
        }

        //return; //move return here

【讨论】:

  • 试过这个方法,只有当文本框内容只在一行中找到时,背景颜色才会改变。第二个问题,尽管文本框内容已更改,但行仍然是彩色的。
  • 第一个问题是因为你放了return。第二个问题是您必须在运行此代码之前将背景色恢复为默认状态。检查更新的答案
  • 无法删除返回,我得到异常。试过:productDataGridView.DefaultCellStyle.BackColor = System.Drawing.Color.White;不会将颜色恢复到默认状态。
  • 没有什么是不可能的。如果方法具有返回类型,则将 return 放在循环之外。您可能需要遍历网格以将其恢复为默认背景色或在循环中添加 else 语句
  • 按照建议删除返回会给出异常:System.NullReferenceException 未处理,最好在 if 语句之前重置行的颜色。这里最大的问题是一行可能会被突出显示而不显示。
【解决方案2】:

尝试了这个解决方案,这终于像一个魅力:

            try
        {
            productDataGridView.ClearSelection();   //or restore rows backcolor to default
            for (int i = 0; i < (productDataGridView.Rows.Count); i++)
            {
                if (productDataGridView.Rows[i].Cells[1].Value.ToString().StartsWith(textBox1.Text, true, CultureInfo.InvariantCulture))
                {
                    productDataGridView.FirstDisplayedScrollingRowIndex = i;
                    productDataGridView.Rows[i].Selected = true; //It is also possible to color the row backgroud
                    return;
                }
            }
        }
        catch (Exception) 
        { 
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2012-03-24
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    相关资源
    最近更新 更多