Binding Data

D

David P. Donahue

Currently, when I bind data to a DataGrid, I use a function as such:

Dim conGrid As SqlClient.SqlConnection
Dim daGrid As SqlClient.SqlDataAdapter
Dim dsGrid As DataSet
Dim stringSQL As String
stringSQL = "SELECT * FROM UserProfiles"
conGrid = New SqlClient.SqlConnection(Global.strDBConnect)
daGrid = New SqlClient.SqlDataAdapter(stringSQL, conGrid)
dsGrid = New DataSet
daGrid.Fill(dsGrid)
DataGrid1.DataSource = dsGrid
DataGrid1.DataBind()

Then, in the DataGrid's Template Columns, controls would have something
like:

text='<%# DataBinder.Eval(Container.DataItem, "Username") %>'

For various reasons, I would like to not have this piece of code in the
..aspx file, but would prefer to have all my VB code running in the
..aspx.vb (code-behind) file. How would I accomplish setting this text
property in this way in my code-behind from the function above? In my
code for handling DataGrid commands (update, for example), I have things
like:

Dim textBoxDay As TextBox
Dim stringDay As String
textBoxDay = e.Item.Cells(3).Controls(1)
stringDay = textBoxDay.Text

Here, "e" is of type DataGridCommandEventArgs and is passed to the
DataGrid1_UpdateCommand function by design. Its "Item" collection is
great for referencing controls in the Template Columns. But, how would
I reference them in the earlier mentioned code?


Regards,
David P. Donahue
(e-mail address removed)
 
K

Ken Cox [Microsoft MVP]

Hi David,

You're going to need to build your own columns, tell them where to get the
data, and add them to the datagrid's Columns collection.

See the sample code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim newCol As New BoundColumn
newCol.HeaderText = "My new column"
newCol.DataField = "Username"
DataGrid1.Columns.AddAt(0, newCol)
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("Username", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
D

David P. Donahue

So far this is working great, thanks. But I could really use the
flexibility of a TemplateColumn instead of a BoundColumn. I added a
TemplateColumn and set its basic properties, but I'm having trouble
adding an ItemTemplate and an EditItemTemplate to that column. I see
the properties, and I see them as type ITemplate, but I'm having trouble
getting the pieces to fit together. Can you show me a basic example of
adding a simple ItemTemplate (just a Label control in there) and a
simple EditItemTemplate (just a TextBox control in there) to a
TemplateColumn?


Gratefully,
David P. Donahue
(e-mail address removed)

Hi David,

You're going to need to build your own columns, tell them where to get
the data, and add them to the datagrid's Columns collection.

See the sample code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim newCol As New BoundColumn
newCol.HeaderText = "My new column"
newCol.DataField = "Username"
DataGrid1.Columns.AddAt(0, newCol)
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("Username", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Currently, when I bind data to a DataGrid, I use a function as such:

Dim conGrid As SqlClient.SqlConnection
Dim daGrid As SqlClient.SqlDataAdapter
Dim dsGrid As DataSet
Dim stringSQL As String
stringSQL = "SELECT * FROM UserProfiles"
conGrid = New SqlClient.SqlConnection(Global.strDBConnect)
daGrid = New SqlClient.SqlDataAdapter(stringSQL, conGrid)
dsGrid = New DataSet
daGrid.Fill(dsGrid)
DataGrid1.DataSource = dsGrid
DataGrid1.DataBind()

Then, in the DataGrid's Template Columns, controls would have
something like:

text='<%# DataBinder.Eval(Container.DataItem, "Username") %>'

For various reasons, I would like to not have this piece of code in
the .aspx file, but would prefer to have all my VB code running in the
.aspx.vb (code-behind) file. How would I accomplish setting this text
property in this way in my code-behind from the function above? In my
code for handling DataGrid commands (update, for example), I have
things like:

Dim textBoxDay As TextBox
Dim stringDay As String
textBoxDay = e.Item.Cells(3).Controls(1)
stringDay = textBoxDay.Text

Here, "e" is of type DataGridCommandEventArgs and is passed to the
DataGrid1_UpdateCommand function by design. Its "Item" collection is
great for referencing controls in the Template Columns. But, how
would I reference them in the earlier mentioned code?


Regards,
David P. Donahue
(e-mail address removed)
 
D

David P. Donahue

Answered my own question :)

http://msdn.microsoft.com/library/d...webservercontroltemplatesprogrammatically.asp


So far this is working great, thanks. But I could really use the
flexibility of a TemplateColumn instead of a BoundColumn. I added a
TemplateColumn and set its basic properties, but I'm having trouble
adding an ItemTemplate and an EditItemTemplate to that column. I see
the properties, and I see them as type ITemplate, but I'm having trouble
getting the pieces to fit together. Can you show me a basic example of
adding a simple ItemTemplate (just a Label control in there) and a
simple EditItemTemplate (just a TextBox control in there) to a
TemplateColumn?


Gratefully,
David P. Donahue
(e-mail address removed)

Hi David,

You're going to need to build your own columns, tell them where to get
the data, and add them to the datagrid's Columns collection.

See the sample code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim newCol As New BoundColumn
newCol.HeaderText = "My new column"
newCol.DataField = "Username"
DataGrid1.Columns.AddAt(0, newCol)
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("Username", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Currently, when I bind data to a DataGrid, I use a function as such:

Dim conGrid As SqlClient.SqlConnection
Dim daGrid As SqlClient.SqlDataAdapter
Dim dsGrid As DataSet
Dim stringSQL As String
stringSQL = "SELECT * FROM UserProfiles"
conGrid = New SqlClient.SqlConnection(Global.strDBConnect)
daGrid = New SqlClient.SqlDataAdapter(stringSQL, conGrid)
dsGrid = New DataSet
daGrid.Fill(dsGrid)
DataGrid1.DataSource = dsGrid
DataGrid1.DataBind()

Then, in the DataGrid's Template Columns, controls would have
something like:

text='<%# DataBinder.Eval(Container.DataItem, "Username") %>'

For various reasons, I would like to not have this piece of code in
the .aspx file, but would prefer to have all my VB code running in
the .aspx.vb (code-behind) file. How would I accomplish setting this
text property in this way in my code-behind from the function above?
In my code for handling DataGrid commands (update, for example), I
have things like:

Dim textBoxDay As TextBox
Dim stringDay As String
textBoxDay = e.Item.Cells(3).Controls(1)
stringDay = textBoxDay.Text

Here, "e" is of type DataGridCommandEventArgs and is passed to the
DataGrid1_UpdateCommand function by design. Its "Item" collection is
great for referencing controls in the Template Columns. But, how
would I reference them in the earlier mentioned code?


Regards,
David P. Donahue
(e-mail address removed)
 
K

Ken Cox [Microsoft MVP]

I wouldn't have known how to implement the TemplateColumn. Thanks for the
link!

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top