Changing datagrid column headers?

mzone

Member
Joined
May 18, 2006
Messages
5
Programming Experience
Beginner
Does anyone know how to Change datagrid column headers after it has loaded from a binded dataset? Pls reply asap thanks y'all!
 
Yes, set new tablestyles, here is an example, there is a DataGrid1 and a Button1 on this form:
VB.NET:
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
  loadDatatable()
End Sub
 
Sub loadDatatable()
  Dim dt As New DataTable("datatbl")
  dt.Columns.Add("col1")
  dt.Columns.Add("col2")
  dt.Columns.Add("col3")
  Dim i As Integer, a, b, c As String
  For i = 1 To 5
    a = "ia" & i.ToString
    b = "ib" & i.ToString
    c = "ic" & i.ToString
    dt.Rows.Add(NewString() {a, b, c})
  Next
  DataGrid1.DataSource = dt
End Sub
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
  changeHeader()
End Sub
 
Sub changeHeader()
  ' Create new Table Style.
  Dim ts As New DataGridTableStyle
  ' Allocate it a mapping name
  ts.MappingName = "datatbl" ' Note this is the same name as the Table
  ' Get rid of the default TableStyle
  DataGrid1.TableStyles.Clear()
  ' Add this Style to the TableStyles Collection
  DataGrid1.TableStyles.Add(ts)
  ' Assign New Widths and Col Header Text to DataGrid columns.
  With DataGrid1.TableStyles("datatbl")
    .GridColumnStyles(0).Width = 100 ' First column, width
    .GridColumnStyles(0).HeaderText() = "column A" ' Heading
    .GridColumnStyles(1).Width = 100 ' First column, width
    .GridColumnStyles(1).HeaderText() = "column B" ' Heading
    .GridColumnStyles(2).Width = 100 ' First column, width
    .GridColumnStyles(2).HeaderText() = "column C" ' Heading
  End With
End Sub
 
This is a perfect example of why DataGrids should no longer be used. Use a DataGridVIEW instead. The amount of additonal functionality is HUGE in comparison and you don't have to spend so much time coding.
 
Agreed here, but DataGridView don't exist in .Net 1.x as were the request.
 
Back
Top