1
votes

I'm filling a datatable from an SQL datareader then..

I've created a datacolumn with value boolean(checkbox) in my data table:

DataTable cDataTable= new DataTable();

DataColumn a = new DataColumn("Select", typeof(bool));

cDataTable.Columns.Add(a);

This add's the checkbox to every row however the checkbox isn't editable

This DataTable is then appended to an ASP GridView:

gridView.DataSource = cDataTable;

I've tried

a.ReadOnly = false;

However this doesn't work either.. i've been reading and it's possible datatables aren't editable after they're filled..

Is there a way to make this check box editable?

2
Are you filling a DataTable then adding a new column to it? Please explain in more detail. - Andy G
Yes it's being filled with a datareader then i'm adding a column to it - i've tried playing with Datagridviews but can't get those checkboxes to work. - db0016
How do you try to edit a check box? - Roman Ieromenko
Sorry by editable I mean I can't "check or uncheck" it when the application is running. It's there and I can set defaultvalue to true or false but can't change the state within the application - db0016

2 Answers

1
votes

I realised that values binded to gridviews are set to ReadOnly and are uneditable within c#

I managed to create an ASP checkbox inside a template field inside the gridview

<columns>
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="CheckBox1" runat="server" checked='false' AutoPostBack="true"/>
</itemtemplate>
</asp:templatefield>
</columns>

I can then get the value this with

(row.Cells[0].FindControl("CheckBox1") as System.Web.UI.WebControls.CheckBox).Checked)
0
votes

I use the following code:

 private void button1_Click(object sender, EventArgs e)
    {
        DataTable cDataTable = new DataTable();

        DataColumn a = new DataColumn("Select", typeof(bool));

        cDataTable.Columns.Add(a);
        for (int i = 0; i < 100; i++)
        {
            cDataTable.Rows.Add(true);
        }
        cDataTable.Rows[5][a] = false;
        dataGridView1.DataSource = cDataTable;
    }

And below is the result. The sixth row is false in "a" columnenter image description here