Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my coding I'm using a gridview with check box and a list box to get the checked items. But the checked items are not displayed in istbox.
I'm using the following code:
C#
protected void btSelect_Click(object sender, EventArgs e)
   {
       StringCollection sc = new StringCollection();
       string id = string.Empty;
       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
           if (cb != null)
           {
               if (cb.Checked)
               {
                   id = GridView1.Rows[i].Cells[1].Text;
                   sc.Add(id);
               }
           }
       }
       foreach (string item in sc)
       {
           productList.Items.Add(item);
       }
   }


Whether I'm missing something?

Thankyou.
Posted
Updated 24-May-12 2:22am
v2

Maybe this will help DataGridDemo[^]
 
Share this answer
 
Comments
Zukiari 24-May-12 8:30am    
While running the code, cb is null so that it fails on the condition-if (cb != null)
[no name] 24-May-12 8:48am    
Do you read the link? Did you see the section titled "Checkboxes and TextBoxes"?
Zukiari 26-May-12 0:15am    
I''ve read that link, but it is not working for me.
Thank you for your response. The link is useful for me in another case. Thanks.
Try:
C#
protected void btSelect_Click(object sender, EventArgs e)
{
        List<string> sc = new List<string>();
        string id = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
            if (cb != null)
            {
                if (cb.Checked)
                {
                    id = GridView1.Rows[i].Cells[1].Text;
                    sc.Add(id);
                }
            }
        }

        productList.DataSource = sc.ToArray();
        productList.DataBind();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900