-2

I want to insert specific row values based on check box click. The design and source is given below. From the design I have to select some Items. If I click approve, I have to store those check box checked row values into DB. Help me to find a proper solution.

Design:

enter image description here

ASPX:

enter image description here

C#:

The given below code is getting the entire gridview value and storing into db. How can I modify this code as per above requirements.

protected void btnApprove_Click(object sender, EventArgs e)
    {

       ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter rs;
       rs = new ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter();

       foreach (GridViewRow row in GridView2.Rows)
        {
            string ItemName = row.Cells[0].Text;
            string Quantity = row.Cells[1].Text;

            rs.testInsert(ItemName, Quantity);
        }
    }
Vipin
  • 251
  • 5
  • 18
  • 43

2 Answers2

3

You need to iterate through your gridview and find check box by Id in cell of current row.

foreach (GridViewRow row in GridView2.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
            bool chk = chkRow.Checked;
            // Do your stuff
        }
    }

Source: http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx

Imad
  • 6,736
  • 10
  • 49
  • 101
1

The given below code is working perfectly.

foreach (GridViewRow row in GridView2.Rows)
    {
      if (row.RowType == DataControlRowType.DataRow)
        {
           CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
             if (chkRow.Checked)
                {
                   string ItemName = row.Cells[0].Text;
                   string Quantity = row.Cells[1].Text;

                   rs.testInsert(ItemName, Quantity);
                }
        }
    }
Vipin
  • 251
  • 5
  • 18
  • 43