Add dynamic gridview checkbox

1

I'm adding the gridview fields via code, but the checkbox does not appear, instead of the checkbox, it appears: "System.Web.UI.WebControls.CheckBox"

Here's how I'm doing:

CheckBox check = new CheckBox();
if (Session["dt1"] != null)
{
    dt1 = (DataTable)Session["dt1"];
}
else
{
    dt1.Columns.Add("ID");
    dt1.Columns.Add("Nome");
    dt1.Columns.Add("Quantidade");
    dt1.Columns.Add("Valor");
    dt1.Columns.Add("Desconto");
    dt1.Columns.Add("Valor Final");
    dt1.Columns.Add("Quitar");
}
dr1 = dt1.NewRow();
dr1["ID"] = txtidprodutoAdd.Text;
dr1["Nome"] = cbProdutoAdd.SelectedItem;
dr1["Quantidade"] = UpQuantidade.Text;
dr1["Valor"] = txtValorAdd.Text;
dr1["Desconto"] = txtDescontoAdd.Text;
dr1["Valor Final"] = txtValorFinalAdd.Text;
dr1["Quitar"] = check;

dt1.Rows.Add(dr1);
GridView5.DataSource = dt1;
GridView5.DataBind();
Session["dt1"] = dt1;

This code works fine when I added the type bool and it was flagged, but now I need the user to have the control mark and uncheck any row in the gridview.

    
asked by anonymous 03.05.2018 / 19:30

1 answer

0

This is because it is making a ToString() in the check (Checkbox) you are adding.

This ToString() is probably being done by the data type of the "Quitar" column of your DataTable .

According to this OS response you need even add the column with type bool .

So, creating the column would look like this:

dt1.Columns.Add(new DataColumn("Quitar", typeof(bool)));

And the user should rather be able to check or uncheck the checkbox.

I hope I have helped.

    
03.05.2018 / 20:01