How to create dynamic CheckBoxes inside a GridView?

北战南征 提交于 2019-12-13 07:31:54

问题


I have a GridView that displays the Approvers list. As shown in below image. I need to show CheckBoxes, if there are multiple Approvers in a column. Is it possible? If yes then how can I achieve it?

E.g The Approvers section has multiple Approver Names in the first row, for which I should show CheckBoxes.

The data displayed in the grid is available in a DataTable and the multiple Approvers are part of a single row hence I can't use TemplateField and display CheckBoxes.


回答1:


Below is the solution. I could achieve this in method OnRowDataBound(). Not sure if this is the best way.

protected void grdApproverDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string ApproverName  = ((Label)e.Row.Cells[2].FindControl("lblANgrd")).Text;

                string[] approvers = ApproverName.Split(';');
                if (approvers.Count() > 1)
                {
                    ((Label)e.Row.Cells[2].FindControl("lblANgrd")).Text = "";

                    int i = 0;
                    foreach (var item in approvers)
                    {
                        CheckBox ckb = new CheckBox();
                        ckb.Text = item;
                        ckb.ID = i.ToString();
                        ckb.ID = "approvernamesdynamic_"+i.ToString();
                        ckb.Checked = true;
                        e.Row.Cells[2].Controls.Add(ckb);
                        i++;
                    }
                }

            }
        }


来源:https://stackoverflow.com/questions/45333248/how-to-create-dynamic-checkboxes-inside-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!