Link to home
Start Free TrialLog in
Avatar of ARACK04
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("System.String")
        dtTable.Columns.Add(newCol)

        newCol = New DataColumn("Division")
        newCol.DataType = System.Type.GetType("System.String")
        dtTable.Columns.Add(newCol)

        newCol = New DataColumn("Total_Value")
        newCol.DataType = System.Type.GetType("System.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()
Avatar of ARACK04
ARACK04

ASKER

Sorry, this line got removed when I posted this:

            row = dtTable.NewRow()
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
Avatar of ARACK04

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 :(
Avatar of Bob Learned
1) I don't see a problem, but here is a simplification for adding columns:

  dtTable = New DataTable()
  dtTable.Columns.Add("Status")
  dtTable.Columns.Add("Division")
  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
Avatar of ARACK04

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()

?????????????
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of ARACK04

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?
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