Promote Your Blog

Monday, January 23, 2012

Operations on DataGridView In Visual Basic - Part 1

1. Adding a Row to DataGridView Programmatically,

Dim col1content,col2content As String
Dim col2content As Integer
2. Clearing contents of all the rows,


3. To read all cells row by row,

For i = 0 To myDataGridView.Rows.Count - 2
' -2 bcoz we are starting with i=0 and last row is empty row added
col1 = myDataGridView.Row(i).Cells(0).Value.ToString()
col2 = myDataGridView.Row(i).Cells(1).Value.ToString()
col3 = myDataGridView.Row(i).Cells(3).Value 'Integer Value

4. Reading a column of selected row,

Dim s As String = myDataGridView .CurrentRow.Cells("ColumnName").Value

5. To change color of text of selected row,

Dim low_score_style As New DataGridViewCellStyle()
low_score_style.ForeColor = Color.Red

myDataGridView .CurrentRow.DefaultCellStyle = low_score_style

6. To change colors of all rows iteratively,
For i = 0 To myDataGridView.Rows.Count - 2
myDataGridView.Rows(i).DefaultCellStyle = low_score_style

7. Alternate way of achieving 6,
myDataGridView.ForeColor = Color.Red

To come up with some of the above tips on DataGridView, I had dived deep into Internet for days to make your life easier ...

No comments:

Post a Comment