使用datagridview的组合框搜索 [英] Combo box search with datagridview

查看:97
本文介绍了使用datagridview的组合框搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在创建Windows应用程序,

我想以主要形式进行搜索,我有一个组合框和数据网格视图
并且我已设置组合框的属性为,
自动完成模式-建议添加
自动完成源-列表项


当我输入单词的第一个字母时,它会显示所有从同一字母开始的单词.
绝对正常.

但是除此之外,当我从组合框中选择该单词建议我时,它也应该在数据网格视图中突出显示或选择.

请帮助我完成此任务!!!
提前致谢. :)

Hi All,

I am creating windows application,

I want to do search in main form, I have one Combo box and data grid view
and i have set property of combo box is,
Auto complete mode - Suggest-append
Auto complete source - List Item


and when i enter first letter of word its showing all word which starting from that same letter.
Its working fine absolutely.

But in addition i want when i select that particular word from combo box suggested me, it should highlight or select in data grid view also.

please help me to complete this task !!!

Thanks In Advance. :)

推荐答案

抱歉,您需要输入:

for(int i = 0; i< dataGridView1.Columns.Count; i ++)
{
for(int j = 0; j< dataGridView1.Rows.Count; j ++)
{
if(dataGridView1 [i,j] .Value!= null)
{
如果(dataGridView1 [i,j] .Value.ToString()== comboBox1.Text)
{
dataGridView1 [i,j] .Style.BackColor = Color.Blue;
//dataGridView1.Rows[j].DefaultCellStyle.BackColor = Color.Red;
}
}
}
}
Ahh sorry You need to put :

for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
for (int j = 0; j< dataGridView1.Rows.Count; j++)
{
if(dataGridView1[i, j].Value!= null)
{
if (dataGridView1[i, j].Value.ToString() == comboBox1.Text)
{
dataGridView1[i, j].Style.BackColor = Color.Blue;
//dataGridView1.Rows[j].DefaultCellStyle.BackColor = Color.Red;
}
}
}
}


您遇到什么问题?您只搜索一列吗?还是可以在DataGridView中的任何位置找到此值?

如果可以在任何列的任何位置找到它,则必须遍历网格中的每一行.然后循环遍历该行中的每个列或单元格.然后检查该单元格的值是否与组合框中的值匹配.如果是这样,那么您将必须执行突出显示或选择单元格的代码.

如果要选择一个单元格,则可以将其.Selected属性设置为True.如果要更改其颜色,则必须使用单元格的.Style.BackColor属性.

以下是一些MSDN文章,可能会对您的项目有所帮助:
DataGridView类 [ DataGridViewCell类 [ DataGridViewCell.Style属性 [
What part are you having trouble with? Are you searching just one column? Or could this value be found anywhere in the DataGridView?

If it can be found anywhere in any column, you''ll have to loop through each row in the grid. Then loop through each column or cell in the row. Then check to see if the value of that cell matches the value in the combobox. If it does, then you''ll have to execute code that highlights or selects the cell.

If you want to select a cell you can just set it''s .Selected property equal to True. If you want to change it''s color you''ll have to work with the cell''s .Style.BackColor property.

Here are some MSDN articles that may help you with your project:
DataGridView Class[^]
DataGridViewCell Class[^]
DataGridViewCell.Style Property[^]

Hope this helps.


嘿,您将需要此功能,并将其放在您的事件中:
假设您有一个combo_box:comboBox1
和一个datagridview:dataGridView1
Hey you will need this, put it in your event :
Assume you have a combo_box : comboBox1
and a datagridview : dataGridView1
for (int i = 0; i < dataGridView1.Columns.Count; i++)
 {
   for (int j = 0; j < dataGridView1.Rows.Count; j++)
      {
         if (dataGridView1 [i, j].Value.ToString()==comboBox1.Text)
           {
               dataGridView1[i, j].Style.BackColor =Color.Yellow; 
              
            /* OR you can use to Highlight all the row :
              dataGridView1.Rows [j].DefaultCellStyle.BackColor = Color.Yellow;
             */
            }
       }
  }


这篇关于使用datagridview的组合框搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆