Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

Plz help me.

i have table named location in this table there are values as showroom1,showroom2,godown etc now i want to load these values in a header column of gridview. how can i do it.

Thanks.
Posted

Bind table to Gridview and set Auto Generated Column = True
 
Share this answer
 
v2
Comments
ulyses31 11-Feb-11 5:09am    
your right. but i think he is referring to the values in his table. not the columns of the table.
you can loop thru your data grid like this:

For Each col as DataGridViewColumn in Me.yourDataGridView.Columns
col.HeaderText = "your table values"
Next

you can get "your table values" by looping thru your dataset. if your using dataset to load your data.
 
Share this answer
 
v2
Thanks, but it shows me column of the table whether i need values of table.

Table <location>

Location_ID Location_Name
1 Showroom1
2 Showroom2
3 Godown
4 etc
5 etc

i need showrrom1, showroom2 etc at gridview header.

Thanks
 
Share this answer
 
Comments
ulyses31 11-Feb-11 5:16am    
have you tried my solution above?
Asghar Sajjad 11-Feb-11 6:09am    
Yeah I have tried your solution but not getting it, please check my code...

Private Sub LoadLocation()

Dim con As SqlConnection
Dim connection As String
'Dim intloop As Integer


Dim str As String = "select locationid,LocationName from location order by locationname asc"

Try
connection = ConfigurationManager.ConnectionStrings("STOCK").ConnectionString
con = New SqlConnection(connection)
con.Open()

Dim dts As New DataSet()
Dim ada As SqlDataAdapter = New SqlDataAdapter(str, con)
Dim ds As New DataSet()
ada.Fill(ds, "Location")

If ds.Tables(0).Rows.Count > 0 Then
If Not IsDBNull(ds.Tables(0).Rows(0).Item("Locationname")) Then
For Each col As DataGridColumn In Me.GridView1.Columns
col.HeaderText = ds.Tables(0).Rows(0).Item("LocationName")
Next
End If
End If

Catch ex As Exception
End Try

End Sub
Sandeep Mewara 11-Feb-11 5:42am    
Not an answer. Use comment feature to respond back.
Asghar Sajjad 11-Feb-11 6:03am    
Thanks ulyses31 please check. i am still not getting values please check my code.

Private Sub LoadLocation()

Dim con As SqlConnection
Dim connection As String
'Dim intloop As Integer


Dim str As String = "select locationid,LocationName from location order by locationname asc"

Try
connection = ConfigurationManager.ConnectionStrings("STOCK").ConnectionString
con = New SqlConnection(connection)
con.Open()

Dim dts As New DataSet()
Dim ada As SqlDataAdapter = New SqlDataAdapter(str, con)
Dim ds As New DataSet()
ada.Fill(ds, "Location")

If ds.Tables(0).Rows.Count > 0 Then
If Not IsDBNull(ds.Tables(0).Rows(0).Item("Locationname")) Then
For Each col As DataGridColumn In Me.GridView1.Columns
col.HeaderText = ds.Tables(0).Rows(0).Item("LocationName")
Next
End If
End If

Catch ex As Exception
End Try

End Sub
ulyses31 11-Feb-11 6:16am    
you also need to loop thru your table to get the values then assign in to col.HeaderText
if no columns yet, you can add columns dynamically based on your table values like this:

VB
If ds.Tables(0).Rows.Count > 0 Then
                For i = 0 To ds.Tables(0).Rows.Count - 1

                     Me.GridView1.Columns.Add("yourColumnName",ds.Tables(0).Rows(i).Item(&quot;locationname&quot;))
                    
                Next
            End If


my previous answers were based on the assumptions that you already have columns and you just want to change the header text of your columns.
 
Share this answer
 
Comments
Asghar Sajjad 12-Feb-11 1:34am    
its giving error too many argument to Public sub Add. i think gv.column.add accept only one argument and we are supplying here 2 arguments, i m not sure.
ulyses31 13-Feb-11 20:48pm    
gv.columns.add can accept 2 arguments.. its an overloaded sub. pls. paste again your complete code.
Asghar Sajjad 14-Feb-11 2:15am    
Here is a code...
Private Sub LoadLocation()

Dim con As SqlConnection
Dim connection, strr As String

Dim str As String = "select * from location"

Try
connection = ConfigurationManager.ConnectionStrings("STOCK").ConnectionString
con = New SqlConnection(connection)
con.Open()

Dim ada As SqlDataAdapter = New SqlDataAdapter(str, con)
Dim ds As New DataSet()
Dim dt As New DataTable
Dim dr As DataRow
ada.Fill(ds, "Location")
ada.Fill(dt)
Dim i As Integer

If ds.Tables(0).Rows.Count > 0 Then
For i = 0 To ds.Tables(0).Rows.Count - 1

Me.GridView1.Columns.Add("locationname", ds.Tables(0).Rows(i).Item("locationname"))


Next
End If

Catch ex As Exception
End Try

End Sub
Ok. I got the solution. Please check the code below.

Private Sub LoadLocation()

Dim con As SqlConnection
Dim connection As String



Dim str As String = "select locationid,LocationName from location order by locationname asc"
lblMsg.Visible = False

connection = ConfigurationManager.ConnectionStrings("STOCK").ConnectionString
con = New SqlConnection(connection)

Dim dts As New DataSet()
Dim ada As SqlDataAdapter = New SqlDataAdapter(str, con)
Dim ds As New DataSet()
ada.Fill(ds)
Dim dd As New DataSet
dd.Tables.Add(New DataTable)
Dim r As New ArrayList

Try
con.Open()
ada = New SqlDataAdapter(str, con)
ada.Fill(dts)
con.Close()
Catch ex As Exception
con.Close()
End Try

For intloop As Integer = 0 To dts.Tables(0).Rows.Count - 1
GVProduct.Columns.Add(New BoundField)
GVProduct.Columns(intloop).HeaderText = dts.Tables(0).Rows(intloop).Item("locationname")
dd.Tables(0).Columns.Add(New DataColumn)
r.Add("")
Next

dd.Tables(0).Rows.Add(r)
GVProduct.DataSource = dd
GVProduct.DataBind()

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