简体   繁体   English

如何在DataTable中添加复选框?

[英]How to add checkbox in DataTable?

I want to add the checkbox in DataTable and the bind ti grid view.我想在 DataTable 和绑定 ti 网格视图中添加复选框。

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?我该怎么做?

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.现在 IsActive 字段将被映射为网格视图上的复选框。

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只需向其添加一个布尔字段并将其绑定到网格视图的复选框字段

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();

You have to Add a field with Boolean type and assign bool value in DataTable and then bind that DataTable with Grid View.您必须添加一个Boolean类型的字段并在 DataTable 中分配 bool 值,然后将该 DataTable 与 Grid View 绑定。

Try to Follow this Link尝试关注此链接

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>

I found another way to solve your problems, after gridview1.DataBind ();我找到了另一种方法来解决你的问题,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;
       }
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM