0

I have insert gridview datas into database using checkbox checked. My prob is when datas present in database checkbox should be checked.

 DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("adminname"), new DataColumn("projectname") });
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox CheckBox1 = (row.Cells[0].FindControl("CheckBox1") as CheckBox);
            if (CheckBox1.Checked == true && CheckBox1 != null)
            {

                string adminname = (row.Cells[1].FindControl("Label1") as Label).Text;
                string projectname = Label6.Text;
                dt.Rows.Add(adminname, projectname);
                SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SUJAY;Data Source=SUPREME6-PC");
                conn.Open();
                String s = "Insert into admintab values('" + adminname + "','" + projectname + "')";
                SqlCommand cmd = new SqlCommand(s, conn);
                cmd.ExecuteNonQuery();
                 CheckBox1.DataBind();
                conn.Close();


            }
            else
            {
                SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SUJAY;Data Source=SUPREME6-PC");
                conn.Open();
                String s = "Delete *from admintab";
                SqlCommand cmd = new SqlCommand(s, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
2
Contributors
1
Reply
12
Views
3 Years
Discussion Span
Last Post by EddyC27
0

first pull data you want from database, just a simple SELECT query, even if it returns no data it will work fine for following:

DataTable dtData = //Data pulled from DB, just use a SELECT statement


    if(dtData.Rows.Count>0)
    {
    checkbox.Checked = true;
    }
    else
    {
    checkbox.Checked = false;
    }

I just say a general SELECt statement because i dont know specifics of data you need to pull. This solution is if, as i understand , your checkbox is not within your grid.

If the checkbox is inside the gridview, it will work best with a bit field inside the database(call it 'Present' or something).
Then in Databound event of grid:

if (e.Row.RowType == DataControlRowType.DataRow)
            {


                Label HdnLbl = e.Row.FindControl("HiddenLabel") as Label;
                CheckBox cbx = e.Row.FindControl("cbxPresent") as CheckBox;
                if (HdnLbl.Text == "")
                {
                    HdnLbl.Text = "True";
                }

                cbx.Checked = bool.Parse(HdnLbl.Text);

            }

Use a Template field within the gridview:

<asp:TemplateField HeaderText="Assured">
                                            <EditItemTemplate>
                                                <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Present") %>' />
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="HiddenLabel" runat="server" Text='<%# Bind("Present") %>' Visible="false"></asp:Label>
                                                <asp:CheckBox ID="cbxPresent" runat="server" Enabled="false" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.