0
<asp:GridView ShowHeaderWhenEmpty="false" ID="gvSubTasks" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
    <Columns>
        <asp:BoundField DataField="SN" HeaderText="SN" />
        <asp:BoundField DataField="ST" HeaderText="ST" />
        <%-- ADD ANOTHER COLUMN HERE WITH THE CHECKBOX --%>
    </Columns>
</asp:GridView>

code-behind:

string strST = @"SELECT 
            ,A345 'SN'
            ,M3w 'ST'
            ,A667 'CT' -- 0 for not complete and 1 for complete
        FROM [mydb].[dbo].[table1]";

public DataTable RT()
{
    using (SqlConnection con = new SqlConnection(gloString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = strST;

            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;

                using (DataSet ds = new DataSet())
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);

                    return dt;
                }
            }
        }
    }
}

gvSubTasks.DataSource = RT();
gvSubTasks.DataBind();

How can I add a checkbox in my gridview per row, with a header "Complete" and based on the CT, pre-select the checkbox if it is 1 and not select the checkbox if it is 0.

2 Answers 2

1

Add this for a column with checkbox

<asp:TemplateField HeaderText="Complete">
  <ItemTemplate>
  <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("CT").ToString() == "1" ? true : false %>' />
  </ItemTemplate>
 <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
Sign up to request clarification or add additional context in comments.

Comments

0

just add

  <asp:CheckBoxField DataField="CT" HeaderText="CT" </asp:CheckBoxField> 

if CT is a CHAR value you can modify your statement like CASE WHEN CT = '1' THEN 1 ELSE 0 END as CT

Comments

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.