vendredi 19 juin 2020

Column of CheckBox in GridView not showing properly

I have this GridView with a column of checkbox

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
                        DataKeyNames="ID" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
                        OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="Non ci sono risultati."
                        Font-Size="Smaller">
                        <Columns>
                            <asp:TemplateField HeaderText="myField">
                                <ItemTemplate>
                                    <asp:Label ID="lblName" runat="server" Checked='<%# Eval("myField") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtName" runat="server" Checked='<%# Eval("myField") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
</asp:GridView>

This is how I pull the data from my SQL SERVER (myField is a bit value)

private void BindGrid()
    {

        string connection = "myConnection";
        cn = new SqlConnection();
        cn.ConnectionString = connection;

        using (SqlConnection con = new SqlConnection(connection))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT ID, myField, FROM MyTable"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }
    }

This is a result of the above functions

enter image description here

As you can see from the image, there is no checkbox in my GridView, so I receive the correct value from my DataBase (that is 1) and I see correctly the value "True" but there is no checkbox? Besides when I hit Edit button I can't edit the field.

Any help is appreciated




Aucun commentaire:

Enregistrer un commentaire