Example of dynamic binding for GridView

  • Thread starter Thread starter Lloyd Sheen
  • Start date Start date
L

Lloyd Sheen

Is there an example of VB code which will take a datasource and extract from
that the needed info (columns etc.) to create the columns and then bind the
data?

This seems so much more difficult to do an ad-hoc query.

Lloyd Sheen
 
Lloyd said:
Is there an example of VB code which will take a datasource and extract from
that the needed info (columns etc.) to create the columns and then bind the
data?

Don't know if this is what you're talking about, but...

AFAIK, The Grid uses runtime type information about public properties
of the elements of a collection to compute the columns:

<aircode>
Public Class Item
Public Property Name As String
'...
End Property

Public Property Age As Integer
'...
End Property

Public Property Country As String
'...
End Property
End Class

'... somewhere else
Dim L As New List(Of Item) 'A strongly typed list

'... Fill the list ...

'Automatically infers the Name, Age and Country cols
DataGridView1.DataSource = L
</aircode>

And it's just as easy with a DataTable (only you get automatic sorting
for free).

HTH.

Regards,

Branco.
 
Branco Medeiros said:
Don't know if this is what you're talking about, but...

AFAIK, The Grid uses runtime type information about public properties
of the elements of a collection to compute the columns:

<aircode>
Public Class Item
Public Property Name As String
'...
End Property

Public Property Age As Integer
'...
End Property

Public Property Country As String
'...
End Property
End Class

'... somewhere else
Dim L As New List(Of Item) 'A strongly typed list

'... Fill the list ...

'Automatically infers the Name, Age and Country cols
DataGridView1.DataSource = L
</aircode>

And it's just as easy with a DataTable (only you get automatic sorting
for free).

HTH.

Regards,

Branco.

Thanks, but I find that I need to get into the datatable and create columns
in the gridview.

Dim cmd As New SqlCommand
cmd.CommandText = Me.QueryText.Text
cmd.Connection = SQLConn

Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet
Dim dt As New DataTable

da.Fill(ds)
dt = ds.Tables(0)


If the following lines are commented out I get no gridview shown on page.

'GridView1.Columns.Clear()

'For Each col As DataColumn In dt.Columns
' Dim c As New BoundField
' c.HeaderText = col.ColumnName
' c.DataField = col.ColumnName
' GridView1.Columns.Add(c)
'Next

GridView1.DataSource = dt
GridView1.DataBind()

If I uncomment them I get data so it appears there is no strongly typed info
to allow the grid to populate without some help.

Lloyd Sheen
 
Lloyd Sheen wrote:
Thanks, but I find that I need to get into the datatable and create columns
in the gridview.
<snip>

Oh, I see. We're probably talking about two completely different
controls. For instance, I don't see a *GridView*, here. What I have is
a *DataGridView*, as in the example below.

<code>
'At form level
Private mData As New DataSet

Private Sub Form1_Shown( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) Handles Me.Shown
Using Con As New SqlClient.SqlConnection, _
Cmd As New SqlClient.SqlCommand, _
Bridge As New SqlClient.SqlDataAdapter
Con.ConnectionString = STR_CONNECTION
Con.Open()

Cmd.Connection = Con
Bridge.SelectCommand = Cmd

Cmd.CommandText = SQL_TABLE1
Bridge.Fill(mData, "Table1")

Cmd.CommandText = SQL_TABLE2
Bridge.Fill(mData, "Table2")

Cmd.CommandText = SQL_TABLE3
Bridge.Fill(mData, "Table3")

Con.Close()
End Using
End Sub

Private Sub LSTTables_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) Handles LSTTables.SelectedIndexChanged

Dim Index As Integer = LSTTables.SelectedIndex
If Index >= 0 Then

'Here, it just works...
GRDitems.DataSource = mData.Tables(Index)

End If

End Sub
</code>

In the example above, GRDItems is the DataGridView, and LSTTables is a
combobox with the list of tables I want to select. And it's VB2005...

HTH.

Regards,

Branco.
 
In ASP, you can do dynamic data biding
GridView1.DataSource = dataTable;
GridView1.DataBind();

In Windows application, there is no GridView1.DataBind(). Does anyone know how to do it? Is there any sample code?

Thanks.
 
Hi! I read your post for GridView's Dynamic Data Binding. This piece of code

GridView1.DataSource = dt
GridView1.DataBind()

is good for ASP application. In Windows application, there is no
GridView1.DataBind()

Do you know how to do it for Windows application? Thanks.
 
Back
Top