devtunis devtunis - 1 month ago 9
C# Question

Designated action to a column in DataGridview C#

enter image description here

this code for add the column 'Check-Out' and make all the Values in Color Green:

DataGridViewButtonColumn column = new DataGridViewButtonColumn
{
FlatStyle = FlatStyle.System;
DefaultCellStyle.ForeColor = Color.ForestGreen;
HeaderText = "Check-Out";
Text = "Check-Out";
UseColumnTextForButtonValue = true;
};

dataGridView1.Columns.Add(column);


I want select for example for id=1, the field 'Check-Out'
... to change the Green 'Check-Out' to Red 'Check-Out'

I have try with this code (but for all fields...):

foreach (DataGridViewRow dr in dataGridView1.Rows)
{
string col1 = dr.Cells["Check-Out"].ToString();

dr.DefaultCellStyle.ForeColor = Color.Red;
}


I have an error: "The column called Check-Out is not found" !!!

Thanks

Answer

You have to give your column a name:

DataGridViewButtonColumn column = new DataGridViewButtonColumn();
column.Name = "Check-Out";
Comments