在DataGridView C#中更改单元格的背景颜色 [英] Change background color of cells in DataGridView C#

查看:304
本文介绍了在DataGridView C#中更改单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个从文本框(txtSearch)搜索到的DatagridView(dgvEmployeeData)。

我想要什么发生在文本框中有文本的时候我希望数据库中显示的所有单元格的背景颜色都改为YellowGreen,当文本框中没有文本时,颜色会变回白色。



我该怎么做?



任何帮助都将不胜感激

Hi,

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

推荐答案

嗨朋友....





你是安静的好问题分析仪。



尝试...来编码...



这是解决方案...



在TextBox的 textChanged事件尝试使用DatagridView的 CellFormatting属性和Event 在输入TextBox后立即更改DataGridView的CELL颜色。





Happ y编程....: - )
Hi Friend....


You are quiet good problem analyzer.

Try...to 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....:-)


你好,



你可以像这样使用该文本框的TextChanged事件。



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;
               }
           }
           else
           {
               foreach (DataGridViewRow dgvrow in dataGridView1.Rows)
               {
                   dgvrow.DefaultCellStyle.BackColor = Color.White;
               }

           }

       }







然后使foreach / for循环设置Datagridview的背景颜色。这里我使用




and then make foreach/ for loop to set the background color of the Datagridview . here i use

Trim()

在文本框中输入值时忽略前导空格。



谢谢

for ignore the leading space while entering value in the textbox .

thanks


我通过添加以下内容解决了左上角单元格未着色的问题:



I fixed the problem of the top left cell being not coloured by adding:

dgvEmployeeData.ClearSelection();





在虚空结束时工作正常



感谢所有帮助



at the end of the void and it works fine

Thanks for all the help


这篇关于在DataGridView C#中更改单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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