devtunis devtunis - 1 year ago 88
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;


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" !!!


Answer Source

You have to give your column a name:

DataGridViewButtonColumn column = new DataGridViewButtonColumn();
column.Name = "Check-Out";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download