Reputation: 385
What I'm trying to here is read from a file chosen in a ComboBox
then loop the file contents adding it to a datagrid, this is working great:
private void ComboBoxLoadPLFFiles_SelectedIndexChanged(object sender, EventArgs e)
{
try {
var eachLine = File.ReadAllLines(@"PotentialLinksFound\" + ComboBoxLoadPLFFiles.Text);
DataGridViewLinks.Rows.Clear();
foreach (var row in eachLine)
{
// Check for forum accounts successfully created ...
string[] cut = row.Split('|');
if (Helpers.CheckForAccountsCreated(cut[0]) == true)
{
//Helpers.ReturnMessage("Found -> " + cut[0]);
DataGridViewLinks.Rows.Add(cut[0], ComboBoxLoadPLFFiles.Text.Replace(".plf", ""), cut[1], "", cut[2]);
DataGridViewLinks.DefaultCellStyle.BackColor = Color.Aqua;
}
else
{
DataGridViewLinks.Rows.Add(cut[0], ComboBoxLoadPLFFiles.Text.Replace(".plf", ""), cut[1], "", cut[2]);
}
}
UpdateLinksCount();
}
catch (Exception ex)
{
Helpers.DebugLogging("[" + DateTime.Now + "]-[" + ex.ToString() + "]");
}
}
My issue is wanted to change the row colour of certain rows depending on if Helpers.CheckForAccountsCreated(cut[0]) == true
returns true (the logic is fine and this works as it should) but the entire gridview turns Aqua instead of a specific row, I could wait until the datagrid is populated the do a foreach but is there no way I can do it when the grid is being populated? any tips would be appreciated I'm possibly overlooking something here.
Upvotes: 0
Views: 28
Reputation: 3197
Have you tried doing something like this:
// get the row that you want to change color
row.DefaultCellStyle.BackColor = Color.Aqua;
// this line here changes all the rows to Aqua
//DataGridViewLinks.DefaultCellStyle.BackColor = Color.Aqua;
When you do this:
DataGridViewLinks.Rows.Add(cut[0], ComboBoxLoadPLFFiles.Text.Replace(".plf", ""), cut[1], "", cut[2]);
Rows.Add() returns the index of the newest row.
We can get this index by doing:
int ind = DataGridViewLinks.Rows.Add(cut[0], ComboBoxLoadPLFFiles.Text.Replace(".plf", ""), cut[1], "", cut[2]);
Now we can change the color of that row like this:
DataGridViewLinks.Rows[ind].DefaultCellStyle.BackColor = Color.Aqua;
Upvotes: 1