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()
ASP.NET

Avatar of undefined
Last Comment
Bob Learned

8/22/2022 - Mon
ARACK04

ASKER
Sorry, this line got removed when I posted this:

            row = dtTable.NewRow()
rhencullen

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
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 :(
Your help has saved me hundreds of hours of internet surfing.
fblack61
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
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
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Bob Learned

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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?
Bob Learned

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