BLOG.CSHARPHELPER.COM: Highlight the DataGridView row that is under the mouse in C#
Highlight the DataGridView row that is under the mouse in C#
Someone recently asked me how to change the style of the row under the mouse in a DataGridView. This example does that. When the program starts, the following code prepares the DataGridView for use.
// The style to use when the mouse is over a row.
private DataGridViewCellStyle HighlightStyle;
private void Form1_Load(object sender, EventArgs e)
{
// Define the highlight style.
HighlightStyle = new DataGridViewCellStyle();
HighlightStyle.ForeColor = Color.Red;
HighlightStyle.BackColor = Color.Yellow;
HighlightStyle.Font = new Font(dgvValues.Font, FontStyle.Bold);
// Make some data items.
dgvValues.Rows.Add(new object[] { "Pencils, dozen", 1.24m, 4 });
dgvValues.Rows.Add(new object[] { "Paper, ream", 3.75m, 3 });
dgvValues.Rows.Add(new object[] { "Cookies, box", 2.17m, 12 });
dgvValues.Rows.Add(new object[] { "Notebook", 1.95m, 2 });
dgvValues.Rows.Add(new object[] { "Pencil sharpener", 12.95m, 1 });
dgvValues.Rows.Add(new object[] { "Paper clips, 100", 0.75m, 1 });
// Calculate totals.
CalculateTotals();
}
// Calculate the total costs and highlight totals greater than $9.99.
private void CalculateTotals()
{
// Calculate the total costs.
foreach (DataGridViewRow row in dgvValues.Rows)
{
// Calculate total cost.
decimal total_cost =
(decimal)row.Cells["PriceEach"].Value *
(int)row.Cells["Quantity"].Value;
// Display the value.
row.Cells["Total"].Value = total_cost;
}
}
The HighlightStyle variable will hold the style used to highlight the row under the mouse. The form's Load event handler defines the style. It then adds some items to the DataGridView and calls the CalculateTotals method.
The CalculateTotals method loops through the DataGridView's rows and displays a total for each.
The program uses the following SetRowStyle method to set a row's style to a particular DataGridViewCellStyle.
// Set the cell Styles in the given row.
private void SetRowStyle(DataGridViewRow row, DataGridViewCellStyle style)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Style = style;
}
}
This code simply loops through the row's cells and sets their Style properties.
The real fun takes place in the DataGridView's cell mouse events. The following code executes when the mouse enters one of the DataGridView's cells.
// The currently highlighted cell.
private int HighlightedRowIndex = -1;
// Highlight this cell's row.
private void dgvValues_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == HighlightedRowIndex) return;
// Unhighlight the previously highlighted row.
if (HighlightedRowIndex >= 0)
{
SetRowStyle(dgvValues.Rows[HighlightedRowIndex], null);
}
// Highlight the row holding the mouse.
HighlightedRowIndex = e.RowIndex;
if (HighlightedRowIndex >= 0)
{
SetRowStyle(dgvValues.Rows[HighlightedRowIndex], HighlightStyle);
}
}
The program stores the index of the currently highlighted row in the variable HighlightedRowIndex. When the mouse enters a cell, the event handler compares the cell's row to HighlightedRowIndex. If the cell is in the currently highlighted row, the correct row is already highlighted so the event handler does nothing else.
If the cell is not in the currently highlighted row, the program calls SetRowStyle to set the currently highlighted row's style to null, effectively unhighlighting the row (if any row is highlighted). The program then updates HighlightedRowIndex and uses SetRowStyle to highlight that row.
The last piece of code executes when the mouse leaves a cell.
// Unhighlight the currently highlighted row.
private void dgvValues_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (HighlightedRowIndex >= 0)
{
SetRowStyle(dgvValues.Rows[HighlightedRowIndex], null);
HighlightedRowIndex = -1;
}
}
If there is a currently highlighted row, this code simply unhighlights it.
Comments