I'm writing a simple app which goes through a folder, takes each file thats in that folder, and sees if it's in a database.
If the file is found in the db, then I do a DataGridView1.Rows.Add(False, file.Name.ToString, folder.Name.ToString, "kb"), which adds the file name and folder name to the new row and keeps it unchecked.
If the file can't be found in the db, then I do a DataGridView1.Rows.Add(True, file.Name.ToString, folder.Name.ToString, "kb"), which is the same as above, except that the checkbox is column 1 is checked.
Now, what I'm wondering about, is how to set the background color of the newly added row
I've tried to do a few things, including:
Me.DataGridView1.Rows(DataGridView1.Rows.Count - 1).DefaultCellStyle.BackColor = Color.Red, hoping that it would set the newly added (last) row's background color if the file wasn't found in the database, and set it to Color.LightGreen if it was found in the db.
Any suggestions would be greatly appreciated.
Thank you,
C. Jason LeBel

Another DataGridView Background Row Color question ....
David Maynard
Thank you so much .... it worked (which I'm sure you already knew).
All it I had to do was change the -1 to -2
Thanks again,
Jay
rbarr8
Please see the code Listed below I hope this will help you out..
For
ICounter = 0 To ds.Tables(0).Rows.Count - 1dgGrid.Rows.Insert(ICounter)
dgGrid.Rows(ICounter).Cells(
"DateEntered").Value = Format(ds.Tables(0).Rows(ICounter)("DateEntered"), "MM/dd/yyyy")dgGrid.Rows(ICounter).Cells(
"UserID").Value = ds.Tables(0).Rows(ICounter)("UserID")dgGrid.Rows(ICounter).Cells(
"Cash").Value = ds.Tables(0).Rows(ICounter)("Cash")dgGrid.Rows(ICounter).Cells(
"Checkt").Value = ds.Tables(0).Rows(ICounter)("Check")dgGrid.Rows(ICounter).Cells(
"Check").Value = ds.Tables(0).Rows(ICounter)("Check")dgGrid.Rows(ICounter).Cells(
"TotalADeposite").Value = ds.Tables(0).Rows(ICounter)("TotalDeposite")IF SomeHow exist Then
dgGrid.Rows(ICounter).DefaultCellStyle.BackColor = Color.Aquamarine
Endif
Next
Freddy Figueroa
Have you considered assigning the cell's color in the CellFormatting event
Umachandar Jayachandran
Just curious though, if DataGridView doesn't really support changing the background color of random rows, is there anything else that does Or would I need to make my own custom control to do it I know I've seen lots of apps around that have grids like this.
Once again, Thanks,
Jay
Udi Azulay
You want to be careful about setting style properties to the DataGridView ad hoc like this. The issue with the grid is, is that the runtime creates a whole new DataGridViewCellStyle object for each customization you make. These objects build up and consume a lot of memory.
So I know so far I haven't answered your question. However because of the above issue, the following is how I do things like set the color.
In my form, I create a distinct DataGridViewCellStyle and assign it the color configuration I want (in my case, this style is for people who are on-line):
this.styleOn = new DataGridViewCellStyle();
this.styleOn.ForeColor = Color.Black;
this.styleOn.BackColor = Color.Yellow;
Then, I have a generic static class that I pass all new rows to:
First, I call that class from my form like ths (the exact method call signature is very specific to my appliction and I'm working on abstacting it, so yours will look different.)
RowFormatter.FormatRow(row1, this.MDIParentForm.AppSettings, IrcBase.CurrentUser, this.styleOwn, this.styleBuddy, this.styleIgnore, this.styleNotify, this.styleOnBuddy, this.styleJoinPart, styleJoinChannel, stylePartChannel, styleIgnorePicture);
and the class:
public class RowFormatter
{
public static void FormatRow(DataGridViewRow row, AppSet appSet, string currentUser, DataGridViewCellStyle styleOwn, DataGridViewCellStyle styleBuddy, DataGridViewCellStyle styleIgnore, DataGridViewCellStyle styleNotify, DataGridViewCellStyle styleOnBuddy, DataGridViewCellStyle styleJoinPart, DataGridViewCellStyle styleJoin, DataGridViewCellStyle stylePart, DataGridViewCellStyle styleIgnorePicture)
{
string text1 = "";
if ((row.Cells[2] != null) && (row.Cells[2].Value != null))
{
text1 = row.Cells[2].Value.ToString().ToUpper().Trim();
}
string text2 = "";
if ((row.Cells[1] != null) && (row.Cells[1].Value != null))
{
text2 = row.Cells[1].Value.ToString().Trim().ToUpper();
}
if (text2 == currentUser.ToUpper())
{
row.DefaultCellStyle = styleOwn;
}
if (((row.Cells[1] != null) && (row.Cells[1].Value != null)) && string.IsNullOrEmpty(row.Cells[1].Value.ToString()))
{
if (row.Cells[2] != null && row.Cells[2].Value != null && !string.IsNullOrEmpty(row.Cells[2].Value.ToString()))
{
if (row.Cells[2].Value.ToString().ToUpper().Contains("HAS LEFT"))
{
row.DefaultCellStyle = stylePart;
}
if (row.Cells[2].Value.ToString().ToUpper().Contains("HAS JOINED"))
{
row.DefaultCellStyle = styleJoin;
foreach (NotifyWordListSettings not in IrcBase.MDIParentForm.AppSettings.NotifyWordListCollection)
{
if (row.Cells[2].Value.ToString().ToUpper().Contains(not.WordToNotify.ToUpper()))
{
row.Cells[2].Style = styleNotify;
}
}
}
}
}
}
}
}
taze
The ony thing about formatting the grid in that manner is that the performance will suffer if you format enough cells like that.
Andrew Cowan
First off, the Add method returns the row index of the newly added row, so use that if you can. Otherwise, If you have AllowUserToAddRows set to true then you need to use: Me.DataGridView1.Rows(DataGridView1.Rows.Count - 2) since -1 identifies the new row. When you do an Add() function the row is added before the new row.
Hope this helps,
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
ParveenDang