更新时间:2023-02-13 16:39:15
使用 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;
useCellValueChanged
andEditingControlShowing
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);
}