如何在GridView中创建动态CheckBoxes? [英] How to create dynamic CheckBoxes inside a GridView?
问题描述
我有一个显示批准者
列表的 GridView
。如下图所示。如果列中有多个批准人
,我需要显示CheckBoxes。可能吗?如果是,那么我该如何实现它?
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.
网格中显示的数据可以在 DataTable
中获得,并且多个批准者
是因此我不能使用 TemplateField
并显示复选框。
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.
推荐答案
以下是解决方案。我可以通过 OnRowDataBound()
来实现。不知道这是最好的方式。
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++;
}
}
}
}
这篇关于如何在GridView中创建动态CheckBoxes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!