Click here to Skip to main content
16,016,391 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a GridView and want to make headers dynamically based on the some SQL query like...

select question from quiz where quizid is 123.

This query will return * number of questions based on the quizid.

How to create headers with the data that's been selected from database?
Posted

This code create the columns according your table column name.

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim oCn As New System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Uid=sa")

       If oCn.State = ConnectionState.Closed Then
           oCn.Open()
       End If
       Me.DataGridView1.Rows.Clear()
       Me.DataGridView1.Columns.Clear()

       Dim cmd As New SqlClient.SqlCommand("select quizid,question from quiz where quizid=123", oCn)
       Dim da As New SqlClient.SqlDataAdapter(cmd)
       Dim ds As New DataSet("bpl")
       Dim i As Integer = 0

       da.Fill(ds, "bpl")

       ''''' CREATE TABLE COLUMNS IN DATAGRID
       If ds.Tables(0).Columns.Count > 0 Then
           While (i <> ds.Tables(0).Columns.Count)
               Me.DataGridView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName, ds.Tables(0).Columns(i).ColumnName)
               i = i + 1
           End While
       End If
       i = 0

       Dim j As Integer = 0
       ''''' DISPLAY THE SELECTED TABLE DATA IN DATAGRID

       If ds.Tables(0).Rows.Count > 0 Then
           While (j <> ds.Tables(0).Rows.Count)
               Me.DataGridView1.Rows.Add()
               While (i <> ds.Tables(0).Columns.Count)
                   Me.DataGridView1.Item(i, j).Value = ds.Tables(0).Rows(j).Item(Me.DataGridView1.Columns(i).HeaderText)
                   i = i + 1
               End While
               i = 0
               j = j + 1
           End While
       End If
       j = 0
   End Sub
 
Share this answer
 
v2
This code create the columns according your data.

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim oCn As New System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Uid=sa")

    If oCn.State = ConnectionState.Closed Then
        oCn.Open()
    End If
    Me.DataGridView1.Rows.Clear()
    Me.DataGridView1.Columns.Clear()

    Dim cmd As New SqlClient.SqlCommand("select question from quiz where quizid=123", oCn)
    Dim da As New SqlClient.SqlDataAdapter(cmd)
    Dim ds As New DataSet("bpl")
    Dim i As Integer = 0

    da.Fill(ds, "bpl")

    ''''' CREATE TABLE COLUMNS IN DATAGRID
    If ds.Tables(0).Columns.Count > 0 Then
        While (i <> ds.Tables(0).Rows.Count)
            Me.DataGridView1.Columns.Add(ds.Tables(0).Rows(i).Item("question").ToString, ds.Tables(0).Rows(i).Item("question").ToString)
            i = i + 1
        End While
    End If

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900