painting DataGridView Row - c#

I am trying to set the background color of a DataGridView row to red. I tried with the following line:
dgvActiveCalls.Rows[1].DefaultCellStyle.BackColor = Color.Red;
but it doesn't work. Then just to see if something is wrong with update I tried to paint the column instead of row:
dgvActiveCalls.Columns[1].DefaultCellStyle.BackColor = Color.Red;
and it worked fine. I would be really thankful if anyone could show the way to paint the DataGridView row.
Thanks!

Make sure that the default style isn't being overriden by another of the styles. Interesting quirk of DGV in 2.0: Seems like the inheritence chain is almost upside down from what you'd expect. If you are dynamically adding columns, your DefaultCellStyle can be ignored and be overridden by the RowsDefaultCellStyle. Sorting can also override styles you've set.
You might want to check in which order you're setting these styles and google for some articles regarding style inheritence.
P.S. Interestingly enough, I googled to find a link to provide, and came across this blog with an almost identical explanation:
http://yakkowarner.blogspot.com/2008/06/datagridview-style-inheritance.html

I tested that on my machine and it worked fine. Are you sure that line isn't highlighted? If it's highlighted you'll see the highlighted color, not the red.

I suggest that you put that code inside of the DataGridView's RowPrePaint event. For example:
private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
/*Here you put your validation in order to paint only the row that you want*/
if (e.RowIndex == 1)
{
DataGridViewRow row = ((DataGridView)sender).Rows[e.RowIndex];
row.DefaultCellStyle.BackColor = Color.Red;
}
}

Related

DataGridViewCellStyle how to style button?

I'm a beginner in C# and I'm trying to style a DataGridView button in Windows Forms.
I don't know how to, as example, remove the border from a button or change the hover color.
Many configurations that are on a normal button are missing in the DataGridView settings.
How can I achieve a full editable button inside DataGridView?
Datagridview works a little different than normal buttons,
but you can still edit several things in it if you search for the subproperties inside the properties. Let's go in a bit of detail:
DefaultCellStyle
Once you've selected your dataGridView, go to properties > RowTemplate. In there, you find something called DefaultCellStyle. if you press on the '...' at the right. then it'll open a popup that allows you to change some of the standard design of the Cells.
The same can also be applied to ColumnHeadersDefaultCellStyle, which is almost the same as DefaultCellStyle.
Columns
You can also go to Columns and add a new Column. After you've added a new column, you're also able to set several properties unique to that Column as well. You can even set all the cells in a Column to act as buttons!
Datagridview has certainely the access to customise, but most of them are divided in the cellstyles and column collections.
Changing the backcolor on hover
This in't as easily done as on a normal button, I did a search and came with this solution:
In properties, at the lightning button, you can see the events, there you can doubleclick on 'CellMouseMove' and add this. (Change the datagridview1 name eventually to be equall to yours)
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Blue;
}
Then doubleclick the 'CellMouseLeave' event so it can revert the color.
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
}
I hope this has helped you further.

visual studio c# DataGridView maintain background color after sort

I populated a DataGridView with a DataTable that contains plain text. Later, I set a specific cell's background color using:
grid.Rows[row].Cells[col].Style.BackColor = setColor;
This works fine until I click on the column sort button in the DataGridView. I would like to know if there is a way to maintain the background color after sorting, irrespective of the text value of the cell. Once I set that cell's background color, it will remember that background color after sorting.
I have seen other examples using the
CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
Event Handler, but the code they write here always seems to have a preconceived notion of what color the cell's background needs to be in relation to the cell's text content (ex: if cellText == "Critical" ...). That will not work in my case, I just need it to remember which cells are set to a specific color.
Any help?
Use the "DataBindingComplete" event !
e.g.
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
if (Convert.ToDateTime(row.Cells["Effective Date"].Value) > DateTime.Now)
{
row.DefaultCellStyle.BackColor = Color.LightYellow;
}
else { row.DefaultCellStyle.BackColor = Color.LightGreen; }
}
Here is what I had to do (thanks to the pointers from Sinatr) : I had to create a second DataTable that is used solely to keep track of which cells are what colors. Then, I had to add a hidden column to the DataGridView which is used as a key. I put the row number of the DataTable in this field so that after it gets sorted, I can use that row-number key to determine the row index into my Color Table. I added code in the DataGridView's CellFormatting to check the Color Table and reapply the color formatting. To make matters a little more complicated, I was having issues on getting the 1st column to become invisible, so I had to ensure that my hidden column was not the first column.
Overall I feel like these steps were hacks to get around the poorly designed and buggy DataGridView. I can't imagine a scenario where you would want to color a cell, and then have it revert colors after sorting. Sorting, in my mind, is just a rearrangement, NOT changing properties or values. I'm sure DataGridView is great and flexible for so many application, but this seems like a fundamental bug or design flaw.
I know this is a little old but the correct way to color DataGridView cells and maintain colors is to use the RowPrePaint event. That way you don't have to re-process the rows "manually" in any future changes to the DataGridView.
What I do is add additional columns or you can use a separate datatable to hold changes. Then using this event doing something similar to this:
private void dgvResults_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
bool.TryParse(dgvResults.Rows[e.RowIndex].Cells["IsChanged"].Value.ToString(), out bool bIsChanged);
if(bisChanged)
dgvUsers.Rows[e.RowIndex].Cells["MyCellName"].Style.BackColor = Color.Salmon;
}

C# Winforms: Customize DataGridView selected gridcolor

I have DataGridView which has 3 columns and 3 rows. If user selects one row, I want to make the grids of that row to change color. I'm completely new to C# and I cant figure out how can I achieve my goal. Please help me out. Thanks
I understand that you are asking about modifying the appearance of the cells' GridLines and doing it individually.
According to MSDN this is possible. However it seems to involve a real big effort. You would need to subclass the DataGridview and modify the example code extensively to work dynamically. After toying with it for a while I decided, that it isn't worth it. I suggest going for one of the other ways to mark the selection. (Or gain a lot of rep and put a bounty on it..)
Looking at the properties of the DataGridView class, you have the option to set the grid color in the DataGridView.GridColor property.
Gets or sets the color of the grid lines separating the cells of the DataGridView.
Example below:
dataGridView1.GridColor = SystemColors.ActiveBorder;
You can change it using DataGridView.GridColor property. This will change the color of the grid lines.
See the link - DataGridView.GridColor
dataGridView1.GridColor = SystemColors.Blue;
This will change every line in the grid. Can you specify what exactly of the row you want to change? I presume you want only the lines of the row. If you want to change only the row lines color, than you can use the following code:
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red;
dataGridView1.Rows[0].DefaultCellStyle.ForeColor = Color.White;
Below you can see it used in CellClick event on the row:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv == null)
return;
if (dgv.CurrentRow.Selected)
{
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red;
dataGridView1.Rows[0].DefaultCellStyle.ForeColor = Color.White;
}
}
Hope this helps you.
DataGridViewRow dgRow = dataGridView1.Rows[e.RowIndex];
dgRow.DefaultCellStyle.BackColor = Color.Red;
dgRow.DefaultCellStyle.ForeColor = Color.Yellow;

Rows in DataGridView are not changing color

I am just trying to put the color red for the back color of the rows that have the value of 4 in a specific column but all the rows have white back color even though there are rows with value more than 4. I also stepped through the code so I know that the code actually execute the code to change the backcolor.
What am I doing wrong here.
dataGridViewMain.DataSource = table;
dataGridViewMain.Sort(dataGridViewMain.Columns["Days in the shop"], ListSortDirection.Descending);
foreach (DataGridViewRow row in this.dataGridViewMain.Rows)
{
if (Convert.ToInt32(row.Cells["Days in the shop"].Value) > 4)
row.DefaultCellStyle.BackColor = Color.Red;
}
Apparently I need to put the logic inside RowPostPaint event. It's all good now.
I have had problems with DataGridView cell colors when creating child forms, it was hard for me to find a solution. If using a child form I had to make sure I changed the color from the Form Load Event. I was initially trying to make changes to color from the main method, which did not work.

Background colour of DataGridViewCheckboxCell

I have a DataGridView bound to a list of objects, and I'm setting a dynamic cell background colour using the CellFormatting event, as in this answer. This works well for every column except the DataGridViewCheckboxColumn. When I click inside this cell (but outside the checkbox) the cell background changes to the default white.
Visually it looks like cell selection is occurring, despite my best efforts to stop it. My cell formatting code sets the SelectionBackColor as well as the BackColor. I've disabled cell selection using the CellStateChanged event, and none of the other columns are selectable:
private void PlayerGrid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
e.Cell.Selected = false;
}
Is there an extra workaround to override the cell behaviour for checkboxes?
I've found a workaround by adding the following code to the CellStateChanged event:
if (e.Cell is DataGridViewCheckBoxCell)
e.Cell.Style.BackColor = BackgroundColor(e.Cell.RowIndex);
(BackgroundColor() calculates the cell background colour based on the row.)
This cures the problem, but could cause performance issues for larger or virtual tables, by causing creation of extra style objects.
I rather like this approach for what I'm doing. It's able to agnostically change background color (including Checkbox) of ANY of the DataGridView cells with a mouse click or Tab--for example purposes--to highlight the currently selected cell. I found other approaches oddly did not color the background of the checkbox as other cell types were colored. In my example, I'm using this approach in the CellFormatting event but I believe a similar syntax can be duplicated with success elsewhere. Also, I believe this more closely answers the OPs question as it relates to, specifically, the CellFormatting event.
void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (W.mf.dgv.CurrentCell != null && e.RowIndex==W.mf.dgv.CurrentCell.RowIndex & e.ColumnIndex==W.mf.dgv.CurrentCell.ColumnIndex)
{
W.mf.dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionBackColor = Color.YellowGreen;
}
else
{
W.mf.dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionBackColor = W.mf.dgv.DefaultCellStyle.SelectionBackColor;
}
}

Categories

Resources