简体   繁体   中英

How to add checkbox in Asp.net Gridview in all header columns

I am new in asp.net developer.

Populated a asp.net grid view with the help of data table and my all columns are dynamic. Now i want to add a check box in my all columns dynamically bot do not want to add in rows. One time i can check only one check box.If i am selecting second time check box in that case first selected check box should be unchecked.

My code is below:

protected void dgvWoList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (e.Row.Cells[i] != null && (!e.Row.Cells[i].IsNullOrEmpty()))
                {
                    CheckBox chk = new CheckBox();
                    chk.Text = e.Row.Cells[i].Text;
                    e.Row.Cells[i].Controls.Add(chk);

                }
            }
        }
    }

Now i am trying to uncheck the check box but unable to do. Plz help me.

Answers: Thanks a lot for your responses. Spl thanks AnthonyBCodes for your advise instead of check box use radio button which solved my problem. I changed my code from check box to radio button as below.

                        RadioButton chk = new RadioButton();
                        chk.GroupName = "radio";
                        chk.Text = e.Row.Cells[i].Text;
                        e.Row.Cells[i].Controls.Add(chk); 

(Answered in the comments and edited into the question by the OP. Move to community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

Thanks a lot for your responses. Special thanks @AnthonyBCodes for your advice instead of a check box use a radio button which solved my problem. I changed my code from a check box to a radio button as below.

                RadioButton chk = new RadioButton();
                chk.GroupName = "radio";
                chk.Text = e.Row.Cells[i].Text;
                e.Row.Cells[i].Controls.Add(chk); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM