0

I have a gridview control in my project in which i wanted to save multiple gridview rows back to the database. How do i get save all rows through looping. Please help me to overcome this problem.

enter image description here

//Save TestDetails

   foreach (GridViewRow rw in GridView1.Rows)
    {
            var n = new TestDetail
            {
                ServiceId = Convert.ToInt32(GridView1.DataKeys[rw.RowIndex].Value),
                Service = rw.Cells[1].Text.ToString(),  //getting input error
                Price = Convert.ToInt32(rw.Cells[2].Text.ToString())  //getting input error
            };

            using (var context = new DiagEntities())
            {
                context.TestDetail.Add(n);
                context.SaveChanges();
            }
     }
1
  • please show the gridview markup Commented Feb 11, 2015 at 9:05

1 Answer 1

1

If you are in edit mode, you could check the control collection of the particular cell... it should context the textbox containing the value.

Service = ((TextBox)(rw.Cells[1].Controls[0]).Text.ToString(),
Price = Convert.ToInt32(((TextBox)(rw.Cells[1].Controls[0]).Text.ToString())
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks "Scartag" for your reply... there is no textbox control associated inside the gridview control.
@Apurba Yeah they won't be till you have an edit mode .. do you get into edit mode before the saving occurs? how do the values change that requires saving?

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.