The
DataGridView
Datagridview cell formatting and image
insertion
I found this works best for
determining when the user enters a row…
I use th DataGridView1_CellEnter
event rather thant the DataGridView1_RowEnter event because I’ve
Found the Row_Enter event to be
buggy.
Private Sub
DataGridView1_CellEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellEnter
If gblRow <> e.RowIndex Then
gblRow = e.RowIndex
LoadTextBoxes(DataGridView1.CurrentRow.Index)
End If
End Sub
Header Style…
DataGridView1.EnableHeadersVisualStyles = False
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.SteelBlue
DataGridView1.Columns("PercentComplete").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns("PercentComplete").HeaderText = "Percent" & vbCrLf & "Complete"
DataGridView1.DataSource = objDs.Tables("Projects")
Visible…
DataGridView1.Columns("Id_Task").Visible = False
Width…
DataGridView1.Columns("Project").Width = 220
Data Alignment…
DataGridView1.Columns("Priority").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Move to a specific cell…
DataGridView1.CurrentCell = DataGridView1.Rows(8).Cells("Priority")
Get the value of a cell…
txtProject.Text = DataGridView1.Rows(eRowIndex).Cells("Project").Value
Set the value of a cell…
Private Sub cmbContract_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbContract.Leave
Try
DataGridView1.BeginEdit(True)
DataGridView1.CurrentRow.Cells("Contract").Value = cmbContract.Text
DataGridView1.EndEdit()
Catch ex As Exception
End Try
End Sub
Check after data entry…
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
DataGridView1.EndEdit()
If DataGridView1.Rows(e.RowIndex).Cells("Priority").Value > 5 Then
MessageBox.Show("Priority Must Be Less Than 5", "Nope", MessageBoxButtons.OK)
End If
End Sub
Change color on rendering…
Private Sub
DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)
Handles DataGridView1.CellFormatting
If DataGridView1.Rows(e.RowIndex).Cells("Priority").Value = 5 Then
DataGridView1.Rows(e.RowIndex).Cells("Priority").Style.BackColor
= Color.Red
Else
DataGridView1.Rows(e.RowIndex).Cells("Priority").Style.BackColor
= Color.White
End If
End Sub
DataGridView -- How to highlight an entire row OnEnter…
Private Sub dataGridView1_RowEnter(ByVal sender As Object,
ByVal e As
DataGridViewCellEventArgs) Handles
DataGridView1.RowEnter
Dim i As Integer
For i = 0 To
dataGridView1.Rows(e.RowIndex).Cells.Count - 1
dataGridView1(i, e.RowIndex).Style.BackColor = Color.Yellow
Next i
End Sub
Private Sub dataGridView1_RowLeave(ByVal sender As
Object, _
ByVal
e As DataGridViewCellEventArgs) Handles
DataGridView1.RowLeave
Dim i As Integer
For i = 0 To
dataGridView1.Rows(e.RowIndex).Cells.Count - 1
dataGridView1(i,
e.RowIndex).Style.BackColor = Color.Empty
Next i
End Sub
Change the order of cells….
With dataGridView1
.Columns("CustomerID").Visible = False
.Columns("ContactName").DisplayIndex = 0
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
.Columns("Country").DisplayIndex = 3
.Columns("CompanyName").DisplayIndex = 4
End With