J
Jonas Andersson
Guest
I have DataGridView and if a cell (in specific column) have a certain value it will change the background of the whole row (the same row where the value exists).
Now I would like to make two changes:
1. Instead of having backround set to yellow I only want the text to change, to red colour. I want to keep the default backround and only manipulate the text.
2. Then I only want to change the text in the cell where the condition is met. So in this case I want "Hello Word!" to be red.
This is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tabControl1.TabPages.Clear();
createGridViews();
}
DataGridView dgwDynamic;
private void createGridViews()
{
int pageNumber = 0;
for (int i = 0; i < 3; i++)
{
//Populate DataGridView.
/* Dynamic DataGridView */
dgwDynamic = new DataGridView();
dgwDynamic.Name = "DataGridView" + i.ToString();
dgwDynamic.ScrollBars = ScrollBars.Both;
dgwDynamic.Dock = DockStyle.Fill;
dgwDynamic.DataBindingComplete += dgwDynamic_DataBindingComplete;
/* Dynamic TabPage */
TabPage tpDynamic = new TabPage();
pageNumber = i + 1;
tpDynamic.Name = "tabPage" + pageNumber.ToString();
string tabName = "TabPage" + pageNumber.ToString();
tpDynamic.Text = tabName;
tpDynamic.TabIndex = i;
// One newGridView per newTabPage
tpDynamic.Controls.Add(dgwDynamic); // Add Dynamic DataGridView to Dynamic TabPage
tabControl1.Controls.Add(tpDynamic); // Add Dynamic TabPage to TabControl
dgwDynamic.DataSource = GetTable(); // Add DataTable to Dynamic DataGridView
}//End for loop
}
private static DataTable table;
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
table = new DataTable();
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(string));
table.Columns.Add("Column3", typeof(string));
// Here we add three DataRows.
table.Rows.Add("Col1_Row1", "Col2_Row1", "Col3_Row1");
table.Rows.Add("Col1_Row2", "Hello World!", "Col3_Row2");
table.Rows.Add("Col1_Row3", "Col2_Row3", "Col3_Row3");
table.AcceptChanges();
return table;
}
private void dgwDynamic_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
List<String> myList = new List<String>();
myList.Add("ABC");
myList.Add("DEF");
myList.Add("Hello World!");
// Format Rows demo
DataGridView dgw = (DataGridView)sender;
if (dgw != null)
{
foreach (DataGridViewRow row in dgw.Rows)
{
//if new row skip, otherwise null reference exception
if (row.IsNewRow)
return;
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex == 1) //Particular column index
{
if (myList.Contains(cell.Value.ToString()))
{
//IF CONDITION IS MET FOR THE CELL SET THE BACKGROUND OF THE ROW TO BE YELLOW
row.DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
//NEED TO CHANGE TEXT INSTEAD OF BACKGROUND AND ONLY THE PARTICULAR CELL THAT CONTAINS THE VALUE
//Some code here to change the text colour in the cell...
}
}
}
}
}
}
}//End partial class
So how do I change my code to have the desired result?
Continue reading...
Now I would like to make two changes:
1. Instead of having backround set to yellow I only want the text to change, to red colour. I want to keep the default backround and only manipulate the text.
2. Then I only want to change the text in the cell where the condition is met. So in this case I want "Hello Word!" to be red.
This is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tabControl1.TabPages.Clear();
createGridViews();
}
DataGridView dgwDynamic;
private void createGridViews()
{
int pageNumber = 0;
for (int i = 0; i < 3; i++)
{
//Populate DataGridView.
/* Dynamic DataGridView */
dgwDynamic = new DataGridView();
dgwDynamic.Name = "DataGridView" + i.ToString();
dgwDynamic.ScrollBars = ScrollBars.Both;
dgwDynamic.Dock = DockStyle.Fill;
dgwDynamic.DataBindingComplete += dgwDynamic_DataBindingComplete;
/* Dynamic TabPage */
TabPage tpDynamic = new TabPage();
pageNumber = i + 1;
tpDynamic.Name = "tabPage" + pageNumber.ToString();
string tabName = "TabPage" + pageNumber.ToString();
tpDynamic.Text = tabName;
tpDynamic.TabIndex = i;
// One newGridView per newTabPage
tpDynamic.Controls.Add(dgwDynamic); // Add Dynamic DataGridView to Dynamic TabPage
tabControl1.Controls.Add(tpDynamic); // Add Dynamic TabPage to TabControl
dgwDynamic.DataSource = GetTable(); // Add DataTable to Dynamic DataGridView
}//End for loop
}
private static DataTable table;
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
table = new DataTable();
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(string));
table.Columns.Add("Column3", typeof(string));
// Here we add three DataRows.
table.Rows.Add("Col1_Row1", "Col2_Row1", "Col3_Row1");
table.Rows.Add("Col1_Row2", "Hello World!", "Col3_Row2");
table.Rows.Add("Col1_Row3", "Col2_Row3", "Col3_Row3");
table.AcceptChanges();
return table;
}
private void dgwDynamic_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
List<String> myList = new List<String>();
myList.Add("ABC");
myList.Add("DEF");
myList.Add("Hello World!");
// Format Rows demo
DataGridView dgw = (DataGridView)sender;
if (dgw != null)
{
foreach (DataGridViewRow row in dgw.Rows)
{
//if new row skip, otherwise null reference exception
if (row.IsNewRow)
return;
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex == 1) //Particular column index
{
if (myList.Contains(cell.Value.ToString()))
{
//IF CONDITION IS MET FOR THE CELL SET THE BACKGROUND OF THE ROW TO BE YELLOW
row.DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
//NEED TO CHANGE TEXT INSTEAD OF BACKGROUND AND ONLY THE PARTICULAR CELL THAT CONTAINS THE VALUE
//Some code here to change the text colour in the cell...
}
}
}
}
}
}
}//End partial class
So how do I change my code to have the desired result?
Continue reading...