Reputation: 2752
I want to add the checkbox in DataTable and the bind ti grid view.
So I try like this.
DataTable dt = new DataTable("UserAcess");
DataColumn dc1 = new DataColumn("PageName");
dt.Columns.Add(dc1);
foreach (var item in RoleName)
{
DataColumn dc = new DataColumn(item.RoleName);
dt.Columns.Add(dc);
}
int i=0, j = 0;
foreach (var page in pageName)
{
i +=1;
DataRow dr = dt.NewRow();
dr["PageName"] = page.PAGE_NAME;
j = 0;
foreach (var role in RoleName)
{
dt.Columns.Add(new DataColumn("che" + i.ToString() + j.ToString(), typeof(System.Web.UI.WebControls.CheckBox)));
j += 1;
CheckBox ck = new CheckBox();
ck.Checked = true;
dr[role.RoleName] = ck;
}
dt.Rows.Add(dr);
}
NewDataGrid.DataSource = dt;
NewDataGrid.DataBind();
But but out put like this
I want to add the check boxes. How can I do it?
Upvotes: 0
Views: 13068
Reputation: 1
I found another way to solve your problems, after gridview1.DataBind (); enter the code below.
gridview1.DataSource = dt;
gridview1.DataBind ()
foreach (GridViewRow row in gridview1.Rows)
{
for (int i = 0; i <= row.Cells.Count - 1; i++)
{
if (i > 1)
{
CheckBox check = (row.Cells[i] as DataControlFieldCell).Controls[0] as CheckBox;
check.Enabled = true;
}
}
}
Upvotes: 0
Reputation: 1
It is necessary that you create an option to enable it. Configurator comes as disabled by default. You can do this as below.
<script type="text/javascript">
function fnSelectAll() {
const disabledAllCheckboxInPage =
Array.from(document.querySelectorAll("input[type='checkbox']"))
disabledAllCheckboxInPage.forEach(item => item.disabled = false)
}
</script>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" type="checkbox" onclick="fnSelectAll()" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Upvotes: 0
Reputation: 27467
change your code to this:
create columns for each role of type boolean
foreach (var item in RoleName)
{
DataColumn dc = new DataColumn(item.RoleName, typeof(bool));
dt.Columns.Add(dc);
}
then change your code where you are stroring checkboxes in datacolumn to store boolean true/false value for column
foreach (var role in RoleName)
{
dr[role.RoleName] = true;
}
Final Code:
DataTable dt = new DataTable("UserAcess");
DataColumn dc1 = new DataColumn("PageName");
dt.Columns.Add(dc1);
foreach (var item in RoleName)
{
DataColumn dc = new DataColumn(item.RoleName, typeof(bool));
dt.Columns.Add(dc);
}
foreach (var page in pageName)
{
DataRow dr = dt.NewRow();
dr["PageName"] = page.PAGE_NAME;
foreach (var role in RoleName)
{
dr[role.RoleName] = true;
}
dt.Rows.Add(dr);
}
NewDataGrid.DataSource = dt;
NewDataGrid.DataBind();
Upvotes: 1
Reputation: 2610
You can do this as below
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:CheckBoxField HeaderText="Select" DataField="IsActive" />
</Columns>
</asp:GridView>
Do not add checkbox field to data table. Just add a boolean field to it and bind it to the checkboxfield of grid view
Upvotes: 1
Reputation: 5636
You have to Add a field with Boolean
type and assign bool value in DataTable and then bind that DataTable with Grid View.
Try to Follow this Link
Upvotes: 0
Reputation: 32729
just add a boolean field in datatable and it will be mapped as checkbox field in datagridview.
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("IsActive", typeof(bool)));
now IsActive field will be mapped as Checkbox on grid view.
Upvotes: 1