ARACK04
asked on
databinding gridView
When I bind a DataTable to a GridView, I am not seeing any data, but instead System.Data.DataRow in each row of the GridView. Here is the code:
Here is a GridView:
<asp:GridView ID="dg2" runat="server" Width="880px" AutoGenerateColumns="false "><Columns >
<asp:BoundField DataField="Division" HeaderText="Division" ItemStyle-Width="150px" />
<asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="125px" ItemStyle-Wrap="true" />
<asp:BoundField DataField="Total_Value" DataFormatString="{0:C}" HeaderText="Total Value" >
</Columns></asp:GridView>
And here is how it gets bound
dtTable = New DataTable()
newCol = New DataColumn("Status")
newCol.DataType = System.Type.GetType("Syste m.String")
dtTable.Columns.Add(newCol )
newCol = New DataColumn("Division")
newCol.DataType = System.Type.GetType("Syste m.String")
dtTable.Columns.Add(newCol )
newCol = New DataColumn("Total_Value")
newCol.DataType = System.Type.GetType("Syste m.Double")
dtTable.Columns.Add(newCol )
'eventually this will be more complex, obviously
For Each key In myKeys
row("Division") = "hello"
row("Status") = "world"
row("Total_Value") = 4.5
dtTable.Rows.Add(row)
Next
dg2.DataSource = dtTable
dg2.DataBind()
Here is a GridView:
<asp:GridView ID="dg2" runat="server" Width="880px" AutoGenerateColumns="false
<asp:BoundField DataField="Division" HeaderText="Division" ItemStyle-Width="150px" />
<asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="125px" ItemStyle-Wrap="true" />
<asp:BoundField DataField="Total_Value" DataFormatString="{0:C}" HeaderText="Total Value" >
</Columns></asp:GridView>
And here is how it gets bound
dtTable = New DataTable()
newCol = New DataColumn("Status")
newCol.DataType = System.Type.GetType("Syste
dtTable.Columns.Add(newCol
newCol = New DataColumn("Division")
newCol.DataType = System.Type.GetType("Syste
dtTable.Columns.Add(newCol
newCol = New DataColumn("Total_Value")
newCol.DataType = System.Type.GetType("Syste
dtTable.Columns.Add(newCol
'eventually this will be more complex, obviously
For Each key In myKeys
row("Division") = "hello"
row("Status") = "world"
row("Total_Value") = 4.5
dtTable.Rows.Add(row)
Next
dg2.DataSource = dtTable
dg2.DataBind()
I'm more of a C# than VB erson, but if you replace the "For each key in mykeys" section with:
Dim dr as DataRow
dr = dt.NewRow()
dr("Division") = "hello"
dr("Status") = "world"
dr("Total_Value") = 4.5
dt.Rows.Add(dr)
You should get one row in your datatable which will then bind to your grid.
Paul
Dim dr as DataRow
dr = dt.NewRow()
dr("Division") = "hello"
dr("Status") = "world"
dr("Total_Value") = 4.5
dt.Rows.Add(dr)
You should get one row in your datatable which will then bind to your grid.
Paul
ASKER
Sorry - I'm getting plenty of rows in the grid, but they all say "System.Data.DataRow". I'm more of a C# person too - I'm stuck upgrading old code :(
1) I don't see a problem, but here is a simplification for adding columns:
dtTable = New DataTable()
dtTable.Columns.Add("Statu s")
dtTable.Columns.Add("Divis ion")
dtTable.Columns.Add("Total _Value", GetType(Double))
2) You have set AutoGenerateColumns = false, which is good.
3) You have defined BoundField entries, which is good.
4) You have defined DataField for each BoundField, which is good.
Bob
dtTable = New DataTable()
dtTable.Columns.Add("Statu
dtTable.Columns.Add("Divis
dtTable.Columns.Add("Total
2) You have set AutoGenerateColumns = false, which is good.
3) You have defined BoundField entries, which is good.
4) You have defined DataField for each BoundField, which is good.
Bob
ASKER
Are there any VB fans out there who know why it works when I change
Dim r = dtTable.NewRow()
to
Dim r As DataRow = dtTable.NewRow()
?????????????
Dim r = dtTable.NewRow()
to
Dim r As DataRow = dtTable.NewRow()
?????????????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, you define to define a new row instance for each key in myKeys:
For Each key In myKeys
Dim row As DataRow = dtTable.NewRow()
row("Division") = "hello"
row("Status") = "world"
row("Total_Value") = 4.5
dtTable.Rows.Add(row)
Next
...
Bob
For Each key In myKeys
Dim row As DataRow = dtTable.NewRow()
row("Division") = "hello"
row("Status") = "world"
row("Total_Value") = 4.5
dtTable.Rows.Add(row)
Next
...
Bob
ASKER
Thanks - god, I hate VB.
I knew I was re-declaring the DataRow on each iteration, but I thought VB would be smart enough to handle the other. Thanks for your help.
How do I turn option strict on?
I knew I was re-declaring the DataRow on each iteration, but I thought VB would be smart enough to handle the other. Thanks for your help.
How do I turn option strict on?
There are two ways:
1) Under options, there is the default for all Projects
2) Add Option Strict On to the top of the module
3) Be prepared for a lot of errors, since Option Strict can cause a lot of noise. You are not allowed to do any late-binding or implicit casts.
Bob
1) Under options, there is the default for all Projects
2) Add Option Strict On to the top of the module
3) Be prepared for a lot of errors, since Option Strict can cause a lot of noise. You are not allowed to do any late-binding or implicit casts.
Bob
ASKER
row = dtTable.NewRow()