Click here to Skip to main content
14,738,277 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)

I have a DatagridView (dgvEmployeeData) that is searched form a textbox (txtSearch).
What I want to happen is when the textbox has text in it I want all the cells in the database that are shown to have their background color changed to YellowGreen and when the textbox has no text in it the color to be changed back to white.

How would I do this?

Any help would be greatly appreciated
joginder-banger 18-Dec-13 22:11pm
Share your coding parts...for more clear what's you want..

Hi Friend....

You are quiet good problem analyzer. code this...

Here is the Solution...

on TextBox's textChanged Event try to use the DatagridView's CellFormatting properties and Event to change the Color of DataGridView's CELL as soon as you typed into TextBox.

Happy Programming....:-)
NattyMan0007 18-Dec-13 23:49pm
Thanks, although I am good at problem anzlying, i'm not too good at problem solving! :)

How would I code what you said. I am very unfamiliar with DataGridView code and properties and I am also new to C# :D
Hello ,

you can use TextChanged event of that textbox like this way .

private void txtenter_TextChanged(object sender, EventArgs e)
           if (txtenter.Text.Trim().Length > 0)
               foreach (DataGridViewRow dgvrow in dataGridView1.Rows)
                   dgvrow.DefaultCellStyle.BackColor = Color.YellowGreen;
               foreach (DataGridViewRow dgvrow in dataGridView1.Rows)
                   dgvrow.DefaultCellStyle.BackColor = Color.White;



and then make foreach/ for loop to set the background color of the Datagridview . here i use
for ignore the leading space while entering value in the textbox .

NattyMan0007 19-Dec-13 1:43am
Thanks that worked perfectly except for the fact that every cell's color is changed except for the top left cell. Any ideas
Animesh Datta 19-Dec-13 1:49am
have you click on the datagridview cell ?
I fixed the problem of the top left cell being not coloured by adding:


at the end of the void and it works fine

Thanks for all the help

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