C# .NET - how to obtain value. - Asked By aman on 26-May-11 06:46 AM

hi all,

i want to get calculated value in below textbox image  on the click of submit button, the value which is inserted in datagridview cells.

in the above image i have coloured the datagridview cells on which i want the aaded value to be inserted in the total amount textbox field on the submit button click.

Thanks;
Regards;
Aman khan
dipa ahuja replied to aman on 26-May-11 07:09 AM
one easiest way is to retrieve the data as:

void bind()
    {
      string q = "select BookName, qty, price , (qty*price) as total from BookDetail";
      SqlDataAdapter da = new SqlDataAdapter(q, "ConnectionString");
 
      DataTable dt = new DataTable();
      da.Fill(dt);
 
      dataGridView1.DataSource = dt;
        dataGridView1.Columns["total"].ReadOnly = true;
    }
Anoop S replied to aman on 26-May-11 07:46 AM
You can multiply like this way also

int total = 0;
   foreach (DataGridViewRow row1 <strong class="highlight">in</strong> dgv1.Rows)
   {
     foreach (DataGridViewRow row2 <strong class="highlight">in</strong> dgv2.Rows)
     {
       if (row1.Index == row2.Index)
       {
         total = (int)row1.Cells[0].Value * (int)row2.Cells[0].Value;
         break;
       }
     }
   }


the "total" local variable holds the result, you can pass that value to corresponding 3rd gridview cell
Jitendra Faye replied to aman on 26-May-11 07:47 AM

For getting total you have to use for each loop for getting value of amount from label.


use this code-

protected void GetTotal_Click(object sender, EventArgs e)

{

int Rowtot = 0;

 foreach (GridViewRow r in GridView2.Rows)

  {

    Label lblTot= (Label)r.FindControl("lblEAmount");

    if (lblTot != null)

   {

      Rowtot = Rowtot + Convert.ToInt32(lblTot.Text);

   }

 }

txtTotal.Text = Rowtot.ToString();
}

TRY THIS CODE AND LET ME KNOW.