0

I am making a shopping cart type of thing using grid view user will keep adding item in it after doing it so user will click save i want to get values from all columns of grid view each row at a time and save it to database.

here is the code for grid view load

  protected void add_Click(object sender, EventArgs e)
{

    product.Item = Item_Drop.SelectedItem.Text;
    product.Quantity = quantity_box.Text;
    int qun =Convert.ToInt32(quantity_box.Text);
    int unitP= Convert.ToInt32(unitPrice_box.Text);
    product.item_Toal = (qun*unitP).ToString();


    list.Add(product);

    temp_gridView.DataSource = list;
    temp_gridView.DataBind();

 }

And here is what i am trying to get values

    public void Values_from_grid()
{ 
  foreach(temp_gridView row in temp_gridView.Rows)
  {
   for(int i = 0; i < temp_gridView.Columns.Count, i++)
   {
    String header = temp_gridView.Columns[i].HeaderText;
    String cellText = row.Cells[i].Text;
   }
   }
 }

i am not getting any values in "header" or "cellText" . .. .

5
  • have you tried GridViewName.rows[0].Cells[0].Text Commented Nov 13, 2013 at 6:05
  • yeah i did, basically the control is not going into the for loop. the Columns count always comes 0, but at same time gridView has 3 rows and 3 columns. Commented Nov 13, 2013 at 6:16
  • adil why you need header text ? Commented Nov 13, 2013 at 6:19
  • header text contains the column name of the database table in which cellText is going to be inserted. Commented Nov 13, 2013 at 6:21
  • i have given the code below please check. Commented Nov 13, 2013 at 7:13

2 Answers 2

1

Use this code to get header row value .

GridViewName.HeaderRow.Cells[0].Text;
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Raghubar, it worked, actually the main problem was the temp_gridView.Columns.Count was not working thats why the control was not going in to the loop i replaced it with a simple int and now every thing is working fine,,,
@AdilWaqar No problem was not temp_gridView.Columns.Count but your foreach loop.However logic for temp_gridView.Columns.Count loop was also wrong.
0

Found Answer no need to use

GridView.Rows.Count

just use a simple int to iterate

int i = 0;
foreach(GridViewRow row in  temp_gridView.Rows)
{

box.Item_id = temp_gridView.Rows[i].Cells[0].Text;
box.Quantity = temp_gridView.Rows[i].Cells[1].Text;
box.Total = temp_gridView.Rows[i].Cells[2].Text;
i++;
}

2 Comments

Is this working ? I mean nothing wrong with the code but how you are saving your data ? in this foreach loop every time you are assigning new values to your objects so when your code iterates for second row your objects loses you previous value as you are assigning new value each time .
Also using column index will get wrong values when you add/delete columns from gridview in future. I suggest you to use template fields in your gridview & use FindControl to find them. Damien.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.