问题
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