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
  • instead of using checkbox in datatable you must use checkbox in grid view and provide the values to checkbox from datatable.. Commented Aug 30, 2013 at 4:25
  • I don't know how to do it. Can you please explain it Commented Aug 30, 2013 at 4:29
  • @user1348351 see my answer below Commented Aug 30, 2013 at 4:37
  • Don't confuse the DataTable class (which is about data), with the CheckBox control (which is about presentation). Commented Aug 30, 2013 at 5:17

6 Answers 6

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please tell me how to enable check box? By default all the check boxes are disable.So user can't edit it. Can you please tell how to enable check box?
Are you trying to edit and save checkbox data? use Edit Command button, when you click edit it should show checkbox in enabled mode.
1

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.

2 Comments

And how will you display checkbox for dynamic columns here using your code above? OP has dynamic columns based on roles.
@rs. OP can just add columns to datattable and re-bind. What is the issue with that?
1

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

Comments

0

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

Comments

0

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>

Comments

0

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;
       }
    }
 }

1 Comment

when in doubt see the link stackoverflow.com/questions/6788922/…. if you need to save the data passed by the check use the link forums.asp.net/t/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.