Splashlin
Splashlin

Reputation: 7585

VB .NET Creating Additional Columns in Gridviews

Im trying to add data into a new column I have created in my gridview. I create the column with the code below:

Dim Field As New BoundField
Field.HeaderText = "Cummulative Amount"
Dim Col As DataControlField = Field
Me.GridView1.Columns.Add(Col)

Now I need to go in and add data for the rows below the column. I have seen ppl saying I need to set it equal to a datafield but how do I create a datafield from scratch then?

Thanks in advance,

Upvotes: 3

Views: 14679

Answers (2)

Jose Basilio
Jose Basilio

Reputation: 51468

Update:

You can create this calculated column right in the markup for the gridview. Assuming that your datasource has an Field named "Amount" and another field named "Quantity" and that your "Cummulative Amount" is a product of those two columns, you could do something like this:

<asp:GridView runat="server" DataSource="sqlDataSource">
  <columns>
    <asp:BoundField HeaderText="Amount" DataField="Amount" />
    <asp:BoundField HeaderText="Quantity" DataField="Quantity" />
    <asp:TemplateField HeaderText="Cummulative Amount">
      <ItemTemplate>
        <asp:Label runat="server" Text='<%# Eval("Amount") * Eval("Quantity") %>' />
      </ItemTemplate>
    </asp:TemplateField>
  </columns>
</asp:GridView>

Upvotes: 1

Cerebrus
Cerebrus

Reputation: 25775

Yes, people have told you the correct thing - You need to set the DataField property of a BoundField in order for it to be rendered. Your DataField would be a public property exposed by the Datasource you bind your GridView to.

As a side point, you don't need to create a DataControlField because the BoundField class inherits from DataControlField. You can directly add the BoundField to the Columns collection of the GridView.

Dim Field As New BoundField()
Field.HeaderText = "Cumulative Amount"
Field.DataField = "CumulativeAmount"

Me.GridView1.Columns.Add(Field)

Upvotes: 4

Related Questions