Question : Adding items to the List through check boxes via Grid view

Hello Experts,

I have a requirements where it needs to add the items in the List by checking the check boxes in the gridview for each and every data row.

Below is the code & screenshot I have been using but for some reasons I could  not able to peform the activity.

It would be great if someone can get this code working. I appreciate your support in advance.

Subbu


 
 
Screenshot of my gridview
340080
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
Multiple select assign.aspx.cs
----------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kelly.StaffTracker.Utility;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class MultipleTeamSelection : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {


        Team objLoadTeams = new Team();
        List<Team> lstAllTeams = objLoadTeams.GetAllTeams();

        if (lstAllTeams != null && lstAllTeams.Count > 0) {

            gvTeamList.DataSource = lstAllTeams;
            gvTeamList.DataBind();


       }
       

    }


     protected void btnAssign_Click(object sender, EventArgs e) {
        // StringBuilder object

        StringBuilder str = new StringBuilder();

        // Select the checkboxes from the GridView control

        for (int i = 0; i < gvTeamList.Rows.Count; i++) {
            GridViewRow row = gvTeamList.Rows[i];
            CheckBox obj = (CheckBox)(row.FindControl("chkSelect"));
            bool isChecked = obj.Checked;


            if (isChecked) {
                // Column 2 is the name column

                str.Append(gvTeamList.Rows[i].Cells[2].Text);
            }
        }

        // prints out the result

        Response.Write(str.ToString());
    }
}

Answer : Adding items to the List through check boxes via Grid view




foreach(GridViewRow row in gvTeamList.Rows)
{
           CheckBox obj = (CheckBox)(row.FindControl("chkSelect"));
           bool isChecked = obj.Checked;
           if (isChecked) {
               // Column 2 is the name column

               str.Append(gvTeamList.Rows[i].Cells[2].Text);
           }
       }

     
and check whether the checkbox name is chkSelect.

Random Solutions  
 
programming4us programming4us