Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagridview Multiple Row headers

Status
Not open for further replies.

omacron

Technical User
Feb 5, 2002
149
Hi I have a DataGridView were I need to have multiple row headers. Like this;

Code:
          C1   C2   C3
Ra   R1
Ra   R2
Rb   R1
Rb   R1

Cannot seem to get it to work. Here is some sample test code I have been working with;

Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim column0 As DataGridViewColumn = New DataGridViewTextBoxColumn()
        column0.DataPropertyName = "C0"
        column0.Name = " "
        DataGridView1.Columns.Add(column0)

        Dim column1 As DataGridViewColumn = New DataGridViewTextBoxColumn()
        column1.DataPropertyName = "C1"
        column1.Name = "C1"
        DataGridView1.Columns.Add(column1)

        Dim column2 As DataGridViewColumn = New DataGridViewTextBoxColumn()
        column2.DataPropertyName = "C2"
        column2.Name = "C2"
        DataGridView1.Columns.Add(column2)

  DataGridView1.RowHeadersWidth = CInt(DataGridView1.RowHeadersWidth * 1.25)
For x As Integer = 1 To 5
    DataGridView1.Rows.Add()
    DataGridView1.Rows(x - 1).HeaderCell.Value = "A" & x.ToString
    DataGridView1.Rows(x).HeaderCell.Value = "B" & x.ToString
Nextt

    End Sub
End Class
 
IMG


Code:
    Public Sub LoadGrid()
        Dim dgv As DataGridView = Me.DataGridView1
        Dim cHeaders() As String = {"C1", "C2", "C3"}
        Dim r1Headers() As String = {"RA", "RB"}
        Dim r2Headers() As String = {"R1", "R2"}
        Dim k As Integer
        For i As Integer = 0 To cHeaders.Count - 1
            dgv.Columns.Add("Col" & cHeaders(i), cHeaders(i))
        Next
        For j As Integer = 0 To r1Headers.Count - 1
            For k = 0 To r2Headers.Count - 1
                dgv.Rows.Add()
                dgv.Rows(dgv.Rows.Count - 2).HeaderCell.Value = r1Headers(j) & " " & r2Headers(k)
            Next
            k = 0
        Next
    End Sub
 
That is not exactly what I need. It needs to be an actual row header and have a visual separation, not just a space.
 
If I understand you are asking for multiple row headers, per row. I may be wrong, but as far as I know there is no native way to do this. The more common thing is multiple rows of column headers, and even that is not directly supported in a datagridview. I do think a flexgrid and other grids support it. There is a lot of examples of code to create multiple rows of column headers, but I never seen multi row headers. I think the way to do a second row header is to create a user defined column something like:

Besides looking like the row header (which should be easy), what other behavior does it need to have? I assume the biggest thing is that its sort is locked to the first row "header" sort.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top