0

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

enter image description here

I want to add the check boxes. How can I do it?

4

6 回答 6

1

只需在数据表中添加一个布尔字段,它将被映射为 datagridview 中的复选框字段。

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("IsActive", typeof(bool))); 

现在 IsActive 字段将被映射为网格视图上的复选框。

于 2013-08-30T04:32:49.930 回答
1

您可以按以下方式执行此操作

<asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:CheckBoxField HeaderText="Select" DataField="IsActive" />
            </Columns>
        </asp:GridView>

不要将复选框字段添加到数据表中。只需向其添加一个布尔字段并将其绑定到网格视图的复选框字段

于 2013-08-30T04:43:57.040 回答
1

将您的代码更改为:

为布尔类型的每个角色创建列

foreach (var item in RoleName)
{   
    DataColumn  dc = new DataColumn(item.RoleName, typeof(bool));
    dt.Columns.Add(dc);                     
}

然后更改您在数据列中存储复选框的代码以存储列的布尔真/假值

foreach (var role in RoleName)
{                  
    dr[role.RoleName] = true;    
}

最终代码:

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();
于 2013-08-30T05:10:09.657 回答
0

您必须在 DataTable 中添加一个Boolean类型并分配 bool 值的字段,然后将该 DataTable 与 Grid View 绑定。

尝试关注此链接

于 2013-08-30T04:34:54.983 回答
0

在 gridview1.DataBind (); 之后,我找到了另一种解决问题的方法;输入以下代码。

 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;
       }
    }
 }
于 2021-03-22T22:20:07.473 回答
0

您必须创建一个选项来启用它。配置器默认禁用。您可以按如下方式执行此操作。

<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>
于 2021-03-18T19:44:06.607 回答