Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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. :)
Posted
Updated 14-Jun-12 3:40am
v4

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;
}
}
}
}
 
Share this answer
 
Comments
ns_27 16-Jun-12 4:07am    
Thank you so much for your help ......
Its working fine
:):D
F. Aro 16-Jun-12 7:40am    
Welcome!!
:)
ns_27 18-Jun-12 3:04am    
Hey can you please help me with one more Question??

In my data grid view previously selected rows remains selected until i go to other page and come on that page again.

I want it like when i search for "Apple" it will get selected and again if i go to com box and search for other word so that previously selected rows get unselected.
F. Aro 19-Jun-12 12:16pm    
It's easy, think a little bit more:
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; */
}
Else dataGridView1[i, j].Style.BackColor =Color.White;//clears the selection
ns_27 23-Jun-12 6:34am    
Thanks again...... I tried "else" earlier with that code but in that i missed out brackets.. so...
Thanks :)
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.
 
Share this answer
 
Hey you will need this, put it in your event :
Assume you have a combo_box : comboBox1
and a datagridview : dataGridView1
C#
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;
             */
            }
       }
  }
 
Share this answer
 
Comments
ns_27 15-Jun-12 2:41am    
Thanks for your help... !!!
I have code this,

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
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.Blue;
dataGridView1.Rows[j].DefaultCellStyle.BackColor = Color.Red;
}

}

}
}

But I am getting Exception as below,,

if (dataGridView1 [i, j].Value.ToString()==comboBox1.Text)----> Nullrefrenceexception was unhadle

How to solve this??
F. Aro 15-Jun-12 7:28am    
Welcome!
You can add before using the code :

If(dataGridView1 [i, j].Value!=null)
{
//here the previous code here
}

This exception is throw because there is some cells not containing a value, so you cannot parse a null value to .toString().

If the solution is done, put your question as solved :-) gdluck
ns_27 16-Jun-12 3:09am    
I have Try this previous code too...
still getting same exception and Error like - "the name 'j' and 'i' does not exist in current contex"

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

if(dataGridView1[i, j].Value!= null)
{
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.Blue;
dataGridView1.Rows[j].DefaultCellStyle.BackColor = Color.Red;
}

}
}

}


}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900