[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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 control. 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[] { "Interview Puzzles Dissected", 15.95m, 1 }); dgvValues.Rows.Add(new object[] { "C# 24-Hour Trainer", 45.00m, 2 }); dgvValues.Rows.Add(new object[] { "Beginning Software Engineering", 45.00m, 5 }); dgvValues.Rows.Add(new object[] { "Essential Algorithms", 60.00m, 3 }); dgvValues.Rows.Add(new object[] { "C# 5.0 Programmer's Reference", 49.99m, 1 }); dgvValues.Rows.Add(new object[] { "Beginning Database Design Solutions", 44.99m, 2 }); // Calculate totals. CalculateTotals(); } // Calculate the total costs. 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 control and calls the CalculateTotals method.

The CalculateTotals method loops through the DataGridView control's rows and displays a total price times quantity for each row.

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 loops through the row's cells and sets their Style properties.

The real fun takes place in the DataGridView cell's mouse events. The following code executes when the mouse enters one of the DataGridView control'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 the new 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.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.