简体   繁体   English

如何在Asp.net Gridview中的所有标题列中添加复选框

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

I am new in asp.net developer. 我是asp.net开发人员的新手。

Populated a asp.net grid view with the help of data table and my all columns are dynamic. 借助数据表填充了一个asp.net网格视图,我的所有列都是动态的。 Now i want to add a check box in my all columns dynamically bot do not want to add in rows. 现在,我想在所有列中添加一个复选框,动态bot不想添加行。 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. Spl感谢AnthonyBCodes的建议,而不是使用复选框单选按钮,它解决了我的问题。 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) ) (已在评论中回答,并由OP编辑成问题。移至社区Wiki答案。请参阅无答案的问题,但问题已在评论中解决(或扩展为聊天)

The OP wrote: OP写道:

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. 特别感谢@AnthonyBCodes的建议,而不是使用复选框来解决我的问题,而不是使用复选框。 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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