输入数据时更改datagridview单元格的颜色。 [英] Change color of a datagridview cell while entering data.

查看:70
本文介绍了输入数据时更改datagridview单元格的颜色。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我想在输入数据的同时改变单元格颜色和样式。

我使用了以下代码但没有工作。请帮帮我。

dataGridView1.Rows.Add(strAccountName).DefaultCellStyle.BackColor = Color.Yellow;

请注意我想改变前面的颜色和粗体。 br />


我尝试了什么:



我试过以下代码但没有工作。请siggest



dataGridView1.Rows.Add(strAccountName).DefaultCellStyle.BackColor = Color.Yellow;

解决方案

< blockquote>使用 CellValueChanged EditingControlShowing 事件,根据您的需要自定义它。



  private   void  dataGridView1_CellValueChanged( object  sender,DataGridViewCellEventArgs e)
{
var cell = dataGridView1.Rows [e.RowIndex ] .Cells [e.ColumnIndex];
cell.Style = new DataGridViewCellStyle(){BackColor = Color.White};
string fontName = Microsoft Sans Serif ;
cell.Style.Font = new 字体(fontName, 8 .25f,FontStyle.Regular ,GraphicsUnit.Pixel);

}



private void dataGridView1_EditingControlShowing( object sender,DataGridViewEditingControlShowingEventArgs e)
{
e.CellStyle.BackColor = Color.Yellow;
e.CellStyle.Font = new 字体(e.CellStyle.Font,FontStyle.Bold);

}


嗨试试这个



私有DataGridViewTextBoxEditingControl withEventsField_EditControl;

private DataGridViewTextBoxEditingControl EditControl {

get {return withEventsField_EditControl; }

设置{

if(withEventsField_EditControl!= null){

withEventsField_EditControl.KeyDown - = EditControl_KeyDown;

}

withEventsField_EditControl = value;

if(withEventsField_EditControl!= null){

withEventsField_EditControl.KeyDown + = EditControl_KeyDown;

}

}

}



private void DGV_EditingControlShowing(对象发送者,System.Windows.Forms。 DataGridViewEditingControlShowingEventArgs e)

{

if(EditControl == null){

EditControl =(DataGridViewTextBoxEditingControl)e.Control;

}

}

private void EditControl_KeyDown(对象发送者,System.Windows.Forms.KeyEventArgs e)

{

e.CellStyle.ForeColor = Color.Red;

e.CellStyle.BackColor = Color.Black;

e.CellStyle.Font = new Font(e .CellStyle.Font,FontStyle.Bold);

}

Dear All,
I want change cell color and style while entering data into it.
I used following code but not work. Please help me.
dataGridView1.Rows.Add(strAccountName).DefaultCellStyle.BackColor =Color.Yellow;
Please note that I want to change fore color and boldness.

What I have tried:

I tried following code but not working. Please siggest

dataGridView1.Rows.Add(strAccountName).DefaultCellStyle.BackColor =Color.Yellow;

解决方案

use CellValueChanged and EditingControlShowing event, customize it depends on your need.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
       {
           var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
           cell.Style = new DataGridViewCellStyle() { BackColor = Color.White };
           string fontName = "Microsoft Sans Serif";
           cell.Style.Font = new Font(fontName, 8.25f, FontStyle.Regular, GraphicsUnit.Pixel);

       }



       private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           e.CellStyle.BackColor = Color.Yellow;
           e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);

       }


Hi try this

private DataGridViewTextBoxEditingControl withEventsField_EditControl;
private DataGridViewTextBoxEditingControl EditControl {
get { return withEventsField_EditControl; }
set {
if (withEventsField_EditControl != null) {
withEventsField_EditControl.KeyDown -= EditControl_KeyDown;
}
withEventsField_EditControl = value;
if (withEventsField_EditControl != null) {
withEventsField_EditControl.KeyDown += EditControl_KeyDown;
}
}
}

private void DGV_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e)
{
if (EditControl == null) {
EditControl = (DataGridViewTextBoxEditingControl)e.Control;
}
}
private void EditControl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
e.CellStyle.ForeColor = Color.Red;
e.CellStyle.BackColor = Color.Black;
e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
}


这篇关于输入数据时更改datagridview单元格的颜色。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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