Add gridview dynamic checkbox

Asked

Viewed 257 times

1

I am adding the gridview fields via code, but the checkbox does not appear, instead the check box appears: "System.Web.UI.WebControls.CheckBox"

Go on like 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 normally when I added the type bool and it was marked, but now I need the user to have control to mark and uncheck any gridview line.

  • You want to add the Checkbox inside the Grid right? If that’s what you need to add a template column, you can’t put it inside the datatable that way.

  • But I need to create the checkbox at the time that adds the data, and I need to add it that way.

  • Yeah, but this way that you’re doing it doesn’t work. Place an object inside the datareader, you have to dynamically add the Itemtemplate column with the Checkbox in your Gridview.

  • https://www.codeproject.com/Questions/366948/How-to-add-checkbox-in-gridview-dynamically check here to see if this is what you need.

  • https://www.codeproject.com/Articles/13462/How-to-create-template-columns-dynamically-in-a-gr

  • @Thiagoloureiro I know how to create it with itemtemplate, but how can I do it so that it is the last column of Grid? 'cause then he’s the first.

Show 1 more comment

2 answers

0

It happens because he’s making one ToString() in the check (Checkbox) you are adding.

That one ToString() is probably being done by column data type "Quitar" of his DataTable.

According to that response from the OS you really need to add the column with the type bool.

So, in creating the column would look like this:

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

And the user must be able to check or deselect the check box.

I hope I’ve helped.

-3

it is necessary that you create an option to enable, because by default it comes Configurator as disabled, You can do this as Below. in Creating the column it would look like this:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.