If dgv.Rows.Count >= jumpToRow AndAlso jumpToRow >= 1 Then
dgv.FirstDisplayedScrollingRowIndex = jumpToRow
dgv.Rows(jumpToRow).Selected = True
End If
Tip 7 - Calculate a column total in the DataGridView and display in a textbox
A common requirement is to calculate the total of a currency field and display it in a textbox. In the snippet below, we will be calculating the total of the ‘Freight’ field. We will then display the data in a textbox by formatting the result (observe the ToString("c")) while displaying the data, which displays the culture-specific currency.
C#
private void btnTotal_Click(object sender, EventArgs e)
{
if(dgv.Rows.Count > 0)
txtTotal.Text = Total().ToString("c");
}
private double Total()
{
double tot = 0;
int i = 0;
for (i = 0; i < dgv.Rows.Count; i++)
{
tot = tot + Convert.ToDouble(dgv.Rows[i].Cells["Freight"].Value);
}
return tot;
}
VB.NET
Private Sub btnTotal_Click(ByVal sender As Object, ByVal e As EventArgs)
If dgv.Rows.Count > 0 Then
txtTotal.Text = Total().ToString("c")
End If
End Sub
Private Function Total() As Double
Dim tot As Double = 0
Dim i As Integer = 0
For i = 0 To dgv.Rows.Count - 1
tot = tot + Convert.ToDouble(dgv.Rows(i).Cells("Freight").Value)
Next i
Return tot
End Function
Tip 8 - Change the Header Names in the DataGridView
If the columns being retrieved from the database do not have meaningful names, we always have the option of changing the header names as shown in this snippet:
C#
private void btnChange_Click(object sender, EventArgs e)
{
dgv.Columns[0].HeaderText = "MyHeader1";
dgv.Columns[1].HeaderText = "MyHeader2";
}
VB.NET
Private Sub btnChange_Click(ByVal sender As Object, ByVal e As EventArgs)
dgv.Columns(0).HeaderText = "MyHeader1"
dgv.Columns(1).HeaderText = "MyHeader2"
End Sub
Tip 9 - Change the Color of Cells, Rows and Border in the DataGridView
C#
private void btnCellRow_Click(object sender, EventArgs e)
{
// Change ForeColor of each Cell
this.dgv.DefaultCellStyle.ForeColor = Color.Coral;
// Change back color of each row
this.dgv.RowsDefaultCellStyle.BackColor = Color.AliceBlue;
// Change GridLine Color
this.dgv.GridColor = Color.Blue;
// Change Grid Border Style
this.dgv.BorderStyle = BorderStyle.Fixed3D;
}
VB.NET
Private Sub btnCellRow_Click(ByVal sender As Object, ByVal e As EventArgs)
' Change ForeColor of each Cell
Me.dgv.DefaultCellStyle.ForeColor = Color.Coral
' Change back color of each row
Me.dgv.RowsDefaultCellStyle.BackColor = Color.AliceBlue
' Change GridLine Color
Me.dgv.GridColor = Color.Blue
' Change Grid Border Style
Me.dgv.BorderStyle = BorderStyle.Fixed3D
End Sub
Tip 10 - Hide a Column in the DataGridView
If you would like to hide a column based on a certain condition, here’s a snippet for that.
C#
private void btnHide_Click(object sender, EventArgs e)
{
this.dgv.Columns["EmployeeID"].Visible = false;
}
VB.NET
Private Sub btnHide_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.dgv.Columns("EmployeeID").Visible = False
End Sub
Tip 11 - Handle SelectedIndexChanged of a ComboBox in the DataGridView
To handle the SelectedIndexChanged event of a DataGridViewComboBox, you need to use the DataGridView.EditingControlShowing event as shown below. You can then retrieve the selected index or the selected text of the combobox.
C#
private void dataGridView1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
ComboBox editingComboBox = (ComboBox)e.Control;
if(editingComboBox != null)
editingComboBox.SelectedIndexChanged += newSystem.EventHandler(this.editingComboBox_SelectedIndexChanged);
}
private void editingComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox1 = (ComboBox)sender;
// Display index
MessageBox.Show(comboBox1.SelectedIndex.ToString());
// Display value
MessageBox.Show(comboBox1.Text);
}
VB.NET
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e AsDataGridViewEditingControlShowingEventArgs)
Dim editingComboBox As ComboBox = CType(e.Control, ComboBox)
If Not editingComboBox Is Nothing Then
AddHandler editingComboBox.SelectedIndexChanged, AddressOfeditingComboBox_SelectedIndexChanged
End If
End Sub
Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e AsSystem.EventArgs)
Dim comboBox1 As ComboBox = CType(sender, ComboBox)
' Display index
MessageBox.Show(comboBox1.SelectedIndex.ToString())
' Display value
MessageBox.Show(comboBox1.Text)
End Sub
Tip 12 - Change Color of Alternate Rows in the DataGridView
C#
private void btnAlternate_Click(object sender, EventArgs e)
{
this.dgv.RowsDefaultCellStyle.BackColor = Color.White;
this.dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.Aquamarine;
}
VB.NET
Private Sub btnAlternate_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.dgv.RowsDefaultCellStyle.BackColor = Color.White
Me.dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.Aquamarine
End Sub
Tip 13 - Formatting Data in the DataGridView
The DataGridView exposes properties that enable you to format data such as displaying a currency column in the culture specific currency or displaying nulls in a desired format and so on.
C#
private void btnFormat_Click(object sender, EventArgs e)
{
// display currency in culture-specific currency for
this.dgv.Columns["Freight"].DefaultCellStyle.Format = "c";
// display nulls as 'NA'
this.dgv.DefaultCellStyle.NullValue = "NA";
}
VB.NET
Private Sub btnFormat_Click(ByVal sender As Object, ByVal e As EventArgs)
' display currency in culture-specific currency for
Me.dgv.Columns("Freight").DefaultCellStyle.Format = "c"
' display nulls as 'NA'
Me.dgv.DefaultCellStyle.NullValue = "NA"
End Sub
Tip 14 – Change the order of columns in the DataGridView
In order to change the order of columns, just set the DisplayIndex property of the DataGridView to the desired value. Remember that the index is zero based.
C#
private void btnReorder_Click(object sender, EventArgs e)
{
dgv.Columns["CustomerID"].DisplayIndex = 5;
dgv.Columns["OrderID"].DisplayIndex = 3;
dgv.Columns["EmployeeID"].DisplayIndex = 1;
dgv.Columns["OrderDate"].DisplayIndex = 2;
dgv.Columns["Freight"].DisplayIndex = 6;
dgv.Columns["ShipCountry"].DisplayIndex = 0;
dgv.Columns["ShipName"].DisplayIndex = 4;
}
VB.NET
Private Sub btnReorder_Click(ByVal sender As Object, ByVal e As EventArgs)
dgv.Columns("CustomerID").DisplayIndex = 5
dgv.Columns("OrderID").DisplayIndex = 3
dgv.Columns("EmployeeID").DisplayIndex = 1
dgv.Columns("OrderDate").DisplayIndex = 2
dgv.Columns("Freight").DisplayIndex = 6
dgv.Columns("ShipCountry").DisplayIndex = 0
dgv.Columns("ShipName").DisplayIndex = 4
End Sub