Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello All,

I want to search data from grid view where i have 3 columns
Name
address
contact number

I have huge database and for search and highlight that i have used below code,

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < datagridviewserach.Columns.Count; i++)
            {
                for (int j = 0; j < datagridviewserach.Rows.Count; j++)
                {
                    if (datagridviewserach[i, j].Value != null)
                    {
                        if (datagridviewserach[i, j].Value.ToString() == comboBox1.Text)
                        {
                            datagridviewserach[i, j].Style.BackColor = Color.Blue;
                            datagridviewserach.Rows[j].DefaultCellStyle.BackColor = Color.Red;
                        }
                        else datagridviewserach[i, j].Style.BackColor = Color.White;
                    }
                }
            }
        }


I have put this same code in all three combo box
but search option is working only on NAME column.
It should worked on 3 column individual.
How to solve this ???
Posted

1 solution

You can have one event for all as below, you can select the combo box on designer and go to events and select ComboBoxSelectedIndexChanged as SelectedIndexChanged event.

Note that you can cast sender as ComboBox and get text.


C#
private void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
	ComboBox comboBox = (ComboBox) sender;
	for (int i = 0; i < datagridviewserach.Columns.Count; i++)
	{
		for (int j = 0; j < datagridviewserach.Rows.Count; j++)
		{
			if (datagridviewserach[i, j].Value != null)
			{
				if (datagridviewserach[i, j].Value.ToString() == comboBox.Text)
				{
					datagridviewserach[i, j].Style.BackColor = Color.Blue;
					datagridviewserach.Rows[j].DefaultCellStyle.BackColor = Color.Red;
				}
				else datagridviewserach[i, j].Style.BackColor = Color.White;
			}
		}
	}
}
 
Share this answer
 
v2
Comments
Sanna_001 25-Jun-12 2:45am    
Hello,

I have changed it for all 3 combo box, but its not working properly.
And can not perform search option on individual combo box.
DamithSL 25-Jun-12 4:48am    
are you get value for comboBox.Text? may be you can try using comboBox.SelectedItem property, debug and see which property you can use for get selected value of comboBox.
Sanna_001 25-Jun-12 7:07am    
I get select values for combo box from database and i have three column and three combo box
brands combo box1
description combo box2
model no combo box3
I want to do search like this if i goto combo box1 and search by brands as same for other two combo box.

and combo box property i have set like,
Autocompletemode Suggest-append
Autocompletesource list item.

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