C# .NET - add two value in gridview

Asked By raj kumar on 27-Aug-10 06:37 AM
add two value in gridview
 gridview column1, row3 value=500
 gridview column2, row3 value=1500
txttotal=2000
give me code
Sandra Jain replied to raj kumar on 27-Aug-10 06:44 AM
You can access the rows of gridview by using row() and columns of gridview by using cell(). Now for this example:

Dim val1 as integer
Dim cell1 as label = DirectCast(GridView1.Row(3).Cells(1).Controls(1), label)
val1=ctype(cell1.text,integer)

Dim val2 as integer
Dim cell2 as label = DirectCast(GridView1.Row(3).Cells(2).Controls(1), label)
val2=ctype(cell2.text,integer)

Dim tot as integer
tot=val1+val2
sri sri replied to raj kumar on 27-Aug-10 07:08 AM
hi,

try the below code to display summary in the gridview


decimal priceTotal = 0;
int quantityTotal = 0;
void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      // add the UnitPrice and QuantityTotal to the running total variables
      priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
      "UnitPrice"));
      quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
      "Quantity"));
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
      e.Row.Cells[0].Text = "Totals:";
      // for the Footer, display the running totals
      e.Row.Cells[1].Text = priceTotal.ToString();
      e.Row.Cells[2].Text = quantityTotal.ToString();
     
      e.Row.Cells[1].HorizontalAlign = _
      e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
      e.Row.Font.Bold = true;
    }
}
Super Man replied to raj kumar on 27-Aug-10 09:09 AM

Just copy paste code , I have written for you

.aspx code:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="called" >

    <Columns>

   

      <asp:DynamicField HeaderText="Total" />

      <asp:BoundField DataField="col1" HeaderText="Col1" SortExpression="col1" />

   

        <asp:BoundField DataField="col2" HeaderText="Col2" SortExpression="col2" />

   

        <asp:BoundField DataField="col3" HeaderText="Col3" SortExpression="col3" />

   

    </Columns>

    </asp:GridView>

.aspx.cs code

    protected void called(object sender, GridViewRowEventArgs e)

    {

     int i = Convert.ToInt16( e.Row.Cells[0].Text);

      int j = Convert.ToInt16(     e.Row.Cells[0].Text);

       int k  = Convert.ToInt16(       e.Row.Cells[0].Text);

     e.Row.Cells[0].Text = (i + j + k).ToString();

    }