Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ASP.NET
<asp:TemplateField HeaderText="Habbies">
                                <ItemTemplate>
                                   <%-- <%#Eval("Habbies") %>--%>
                                         <asp:Label ID="lblHabbies" runat="server" Text='<%# Eval("Habbies")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                        <asp:CheckBoxList ID="chbxlist" runat="server" AutoPostBack="true" Enabled="true">
                                        <asp:ListItem>singing</asp:ListItem>
                                         <asp:ListItem>reading</asp:ListItem>
                                         <asp:ListItem>watchingtv</asp:ListItem>
                                          <asp:ListItem>gardening</asp:ListItem>
                               </asp:CheckBoxList>
                                </EditItemTemplate>
                            </asp:TemplateField>



this my gridview.


C#
How to retrieve selected checkbox values from database in asp.net



in this condition I need a Gridview ..if i am clicking edit button in gridview automatically bind the checkboxs values in gridview from DataBase ..


ia m trying this
C#
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    int genderValue = (int)DataBinder.Eval(e.Row.DataItem, "Gender");
        //    RadioButtonList rb = (RadioButtonList)e.Row.FindControl("radioGender");
        //    rb.Items.FindByValue(genderValue.ToString()).Selected = true;
        //}
        //if (e.Row.RowType == DataControlRowType.DataRow && gridview1.EditIndex == e.Row.RowIndex)
        //{
        //    RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender");
        //    string query = "SELECT * FROM employee";
        //    SqlCommand cmd = new SqlCommand(query,con);
        //    SqlDataAdapter da = new SqlDataAdapter(cmd);
        //    DataTable dt = new DataTable();
        //    da.Fill(dt);
        //    rbGender.DataSource = dt;
        //    rbGender.DataTextField = "Gender";
        //    rbGender.DataValueField = "Name";
        //    rbGender.DataBind();
        //    rbGender.Items.FindByValue((e.Row.FindControl("lblGender") as Label).Text).Selected = true;
        //}
        
        //string Habbies = string.Empty;
        //for (int i = 0; i < chbxlist.Items.Count; i++)
        //{
        //    if (chbxlist.Items[i].Selected)
        //    {
        //        Habbies = chbxlist.Items[i].Text;
        //        Update(Habbies, chbxlist.Items[i].Selected);
        //    }
        //}

        DataRowView dRowView = (DataRowView)e.Row.DataItem;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

                RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender");

                DropDownList dropdownStatus = (DropDownList)e.Row.FindControl("dropdownStatus");
                CheckBoxList chbxlist = (CheckBoxList)e.Row.FindControl("chbxlist");
                rbGender.SelectedValue = dRowView[5].ToString();
                dropdownStatus.SelectedValue = dRowView[7].ToString();
                chbxlist.SelectedValue = dRowView[6].ToString();

            }
        }


What I have tried:

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
// int genderValue = (int)DataBinder.Eval(e.Row.DataItem, "Gender");
// RadioButtonList rb = (RadioButtonList)e.Row.FindControl("radioGender");
// rb.Items.FindByValue(genderValue.ToString()).Selected = true;
//}
//if (e.Row.RowType == DataControlRowType.DataRow && gridview1.EditIndex == e.Row.RowIndex)
//{
// RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender");
// string query = "SELECT * FROM employee";
// SqlCommand cmd = new SqlCommand(query,con);
// SqlDataAdapter da = new SqlDataAdapter(cmd);
// DataTable dt = new DataTable();
// da.Fill(dt);
// rbGender.DataSource = dt;
// rbGender.DataTextField = "Gender";
// rbGender.DataValueField = "Name";
// rbGender.DataBind();
// rbGender.Items.FindByValue((e.Row.FindControl("lblGender") as Label).Text).Selected = true;
//}

//string Habbies = string.Empty;
//for (int i = 0; i < chbxlist.Items.Count; i++)
//{
// if (chbxlist.Items[i].Selected)
// {
// Habbies = chbxlist.Items[i].Text;
// Update(Habbies, chbxlist.Items[i].Selected);
// }
//}

DataRowView dRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{

RadioButtonList rbGender = (RadioButtonList)e.Row.FindControl("rbGender");

DropDownList dropdownStatus = (DropDownList)e.Row.FindControl("dropdownStatus");
CheckBoxList chbxlist = (CheckBoxList)e.Row.FindControl("chbxlist");
rbGender.SelectedValue = dRowView[5].ToString();
dropdownStatus.SelectedValue = dRowView[7].ToString();
chbxlist.SelectedValue = dRowView[6].ToString();

}
}
Posted
Updated 30-May-16 0:52am
v3
Comments
aarif moh shaikh 30-May-16 5:59am    
unclear..

1 solution

Hi,

try this

ASP.NET
<asp:CheckBox Id="CheckBox1" Runat="Server" Checked='<%# Convert.ToBoolean(Eval("DBTblColumnName"))%>'/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900