RowHeader Column in WinForm DatagridView


RowHeader Column in WinForm DatagridView This code gives the details how to build Rowheader column with user defined Header text and column values.

In Winforms, Datagridview does not give direct way to customise first column i.e. Rowheaders. Following Code gives the details how to build Rowheader column with user defined Header text and column values.


Public Class frmDataGridView

Private Sub frmDataGridView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dummy Data to fill the grid
Dim dt As New DataTable()
dt.Columns.Add("FirstName")
dt.Columns.Add("LastName")

For j As Integer = 1 To 6
dt.Rows.Add("DotNet" & j.ToString(), "DotNet" & j.ToString())
Next

Me.DataGridView1.DataSource = dt
Me.DataGridView1.Columns(0).Width = 70

'Header text for first column
DataGridView1.TopLeftHeaderCell.Value = "Sr.No."
'To hide the triangular arrow
Me.DataGridView1.RowHeadersDefaultCellStyle.Padding = New Padding(3)
'To hide the last row(Default newrow)
DataGridView1.AllowUserToAddRows = False

'Generate row numbers
AddRowHeaders()
End Sub

Private Sub AddRowHeaders()
' Add values to rowheader cell.
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).HeaderCell.Value = (i + 1).ToString()
DataGridView1.Rows(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
Next i
End Sub

Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
AddRowHeaders()
End Sub
End Class


Attachments

Comments

Author: PHANI HARSHITHA MADALA11 Feb 2010 Member Level: Gold   Points : 1

Hi Mr.Kiran,

Very usefull info..

Thanks for sharing with all of us.


regards,

harshitha



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: