Click here to Skip to main content
15,886,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir I am using a checkboxlist in my gridview edit item template.
There is a problem. I am not getting the previously checked value in my checkboxlist in edititem template on edit mode. I want to update records.
Please help. Thanks in advance..

value is saved in this format in database
"Gym ,Park ,Lift ,Security ,Club House ,Swimming pool ,Water Storage ,Power Backup ,Reserve Parking ,Vaastu Complaint" and i want on edit command of gridview these values to bind in checkboxlist...


Update code and query

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string Property_ID = Convert.ToString(GridView1.DataKeys[e.RowIndex].Values["Property_ID"]);

        DropDownList ddlpropfor = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlpropfor");
        string propfor = Convert.ToString(ddlpropfor.SelectedItem);

        TextBox PName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpname");
        string pname = PName.Text;

        DropDownList PropertyType = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlpropertytype");
        string ptype = Convert.ToString(PropertyType.SelectedItem);

        DropDownList PropertyAge = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlpropertyage");
        string pAge = Convert.ToString(PropertyAge.SelectedItem);


        DropDownList Postedby = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlpostedby");
        string postedby = Convert.ToString(Postedby.SelectedItem);


        DropDownList City = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlcity");
        string city = Convert.ToString(City.SelectedItem.Value);

        DropDownList Locality = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddllocality");
        string locality = Convert.ToString(Locality.SelectedItem.Value);

        TextBox Address = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress");
        string address = Address.Text;

        TextBox Pincode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpincode");
        string pincode = Pincode.Text;

        TextBox Price = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtprice");
        string price = Price.Text;

        DropDownList Bedroom = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlbedrooms");
        string bedroom = Convert.ToString(Bedroom.SelectedItem);

        TextBox Area = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtarea");
        string area = Area.Text;

        CheckBoxList Ame = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("chklamenities");
        string ame = Convert.ToString(Ame.SelectedItem);


        TextBox Features = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtfeatures");
        string features = Features.Text;

        TextBox Date = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");
        DateTime date = Convert.ToDateTime(Date.Text);
        //DateTime date = DateTime.TryParse(Date.Text); 

        TextBox ImageName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtimgname");
        string imgname = ImageName.Text;

        TextBox ImagePath = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtimgpath");
        string imgpath = ImagePath.Text;

        
        TextBox Name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
        string name = Name.Text;

        TextBox Email = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtemail");
        string email = Email.Text;

        TextBox Mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmob");
        string mobile = Mobile.Text;

        TextBox PriceWords = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpwords");
        string pricewords = PriceWords.Text;

        CheckBox Active = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("chkactive");
        string active = Convert.ToString(Active.Checked);


        SqlConnection con = new SqlConnection(str);
        string strQ = "update tbl_Post_Property set Property_For = '" + propfor + "',Property_Name='" + pname + "',Property_Type = '" + ptype + "',Property_Age = '" + pAge + "',Posted_By='" + postedby + "',City_Id = '" + city + "',Locality_Id='" + locality + "',Address = '" + address + "',Pincode = '" + pincode + "',Price='" + price + "',Bedroom='" + bedroom + "',Area='" + area + "',Amenities='" + ame + "',Key_Features='" + features + "',Date_Posted='" + date.ToString("dd-MMM-yyyy") + "',ImageName='" + imgname + "',ImagePath='" + imgpath + "',Name='" + name + "',Email='" + email + "',Mobile='" + mobile + "',PriceWords='" + pricewords + "', IsActive = '" + active + "'  where Property_ID='" + Property_ID + "'";
        SqlCommand cmd = new SqlCommand(strQ, con);
        con.Open();
        cmd.ExecuteNonQuery();
        lblmessage.Text = "Record Updated Successfully";
        GridView1.EditIndex = -1;
        Filldata();
    }




RowDatabound Code
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList propertyfor = (DropDownList)e.Row.FindControl("ddlpropfor");
                propertyfor.SelectedItem.Text = Convert.ToString(drv[1]);

                DropDownList ddltype = (DropDownList)e.Row.FindControl("ddlpropertytype");
                ddltype.SelectedValue = Convert.ToString(drv[3]);

                DropDownList ddlage = (DropDownList)e.Row.FindControl("ddlpropertyage");
                ddlage.SelectedItem.Text = Convert.ToString(drv[4]);

                DropDownList ddlpostedby = (DropDownList)e.Row.FindControl("ddlpostedby");
                ddlpostedby.SelectedItem.Text = Convert.ToString(drv[5]);

                DropDownList ddlcity = (DropDownList)e.Row.FindControl("ddlcity");
                ddlcity.SelectedValue = Convert.ToString(drv[6]);

                DropDownList ddllocality = (DropDownList)e.Row.FindControl("ddllocality");
                ddllocality.SelectedValue = Convert.ToString(drv[7]);

                
                DropDownList ddlbedroom = (DropDownList)e.Row.FindControl("ddlbedrooms");
                ddlbedroom.SelectedItem.Text = Convert.ToString(drv[11]);

                //CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("chklamenities");
                //chkb.SelectedValue = drv[13].ToString();

                CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("chklamenities");
                string ame = "";
                foreach (ListItem li in chkb.Items)
                {
                    if (li.Selected)
                    {
                        ame += li.Text + ",";
                    }

                }
                ame = ame.TrimEnd(',');


                CheckBox chk = (CheckBox)e.Row.FindControl("chkactive");
                chk.Checked = Convert.ToBoolean(drv[22]);

                
               


            }
        }
    }

This is ths code how I am saving checkboxlist values from front end to database
C#
String Ame = "";
                for (int i = 0; i < chklamenities.Items.Count; i++)
                {
                    if (chklamenities.Items[i].Selected)
                    {
                        Ame += chklamenities.Items[i].Value + " ,";
                    }
                }
                Ame = Ame.TrimEnd(',');


this my gridview design
XML
<asp:TemplateField ControlStyle-Width="600px" HeaderText="Amenities">
                       <ItemTemplate>
                           <asp:Label ID="lblame" runat="server" Text='<%#Eval("Amenities")%>'></asp:Label>
                       </ItemTemplate>
                       <EditItemTemplate>
                      <asp:CheckBoxList ID="chklamenities" runat="server" CellPadding="1" RepeatColumns="5"
                   RepeatDirection="Horizontal">
                   <asp:ListItem>Gym</asp:ListItem>
                   <asp:ListItem>Park</asp:ListItem>
                   <asp:ListItem>Lift</asp:ListItem>
                   <asp:ListItem>Security</asp:ListItem>
                   <asp:ListItem>Club House</asp:ListItem>
                   <asp:ListItem>Swimming pool</asp:ListItem>
                   <asp:ListItem>Water Storage</asp:ListItem>
                   <asp:ListItem>Power Backup</asp:ListItem>
                   <asp:ListItem>Reserve Parking</asp:ListItem>
                   <asp:ListItem>Vaastu Complaint</asp:ListItem>
               </asp:CheckBoxList>
                       </EditItemTemplate>
                   </asp:TemplateField>
Posted
Updated 21-Nov-12 21:26pm
v7

Hi Raj,

Update your code as below.

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   DataRowView drv = e.Row.DataItem as DataRowView;
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      if ((e.Row.RowState & DataControlRowState.Edit) > 0)
      {
         CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("chklamenities");
         string ame="";
         foreach (ListItem li in chkame.Items)
         {
            if(li.Selected)                    
            {
                ame += li.Text + ",";
            }
                    
         }
         ame = ame.TrimEnd(",");
      }
   }
}
 
Share this answer
 
Comments
Raj.Rautela 22-Nov-12 3:17am    
sir i tried what u told but still not working for me.i'hv updated my question pls have a look...thanks sir
Mohd. Mukhtar 22-Nov-12 3:21am    
Please update full methos where you are have written update query.
string strQ = "update tbl_Post_Property set Amenities='" + ame + "' where Property_ID='" + Property_ID + "'";
Raj.Rautela 22-Nov-12 3:28am    
sir i'hv updated my question have a look
Mohd. Mukhtar 22-Nov-12 3:33am    
Raj you should try below code.
in place of CheckBoxList Ame = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("chklamenities");
string ame = Convert.ToString(Ame.SelectedItem

you need to try
CheckBoxList Ame = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("chklamenities");
string ame="";
foreach (ListItem li in chkame.Items)
{
if(li.Selected)
{
ame += li.Text + ",";
}

}
ame = ame.TrimEnd(",");
Raj.Rautela 22-Nov-12 3:59am    
sir for update it worked but onedit command previously entered values are not retrived in checkboxlist
Please see the below row data bound method.

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   DataRowView drv = e.Row.DataItem as DataRowView;
   if (e.Row.RowType == DataControlRowType.DataRow)
   { 
      string Property_ID = Convert.ToString(GridView1.DataKeys[e.RowIndex].Values["Property_ID"]);
      CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("chklamenities");
      string query = "Select Amenities FROM tbl_Post_Property where Property_ID='" + Property_ID + "'";
      SqlConnection con = new SqlConnection(str);
      SqlCommand cmd = new SqlCommand(query, con);
      con.Open();
      DataSet ds = new DataSet();
      SqlDataAdaptor da= new SqlDataAdapter(cmd);
      da.fill(ds);
      if(ds.Table[0].Rows.Count>0)
         string ame = ds.Table[0].Rows[0]["Amenities"];
      foreach (ListItem li in chkb)
      {
        if(ame.Contains(li.Text))                    
        {
           li.Selected = true;        
        }                    
      }
    }
   }
}
 
Share this answer
 
v2
Comments
Raj.Rautela 22-Nov-12 5:33am    
sorry sir its not working for me...
Mohd. Mukhtar 22-Nov-12 5:38am    
What error you are getting here?
Raj.Rautela 22-Nov-12 5:46am    
there are 3 errors
1)
'System.Web.UI.WebControls.GridViewRowEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewRowEventArgs' could be found (are you missing a using directive or an assembly reference?)

2)
'System.Data.DataSet' does not contain a definition for 'Table' and no extension method 'Table' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?)


3)
foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.CheckBoxList' because 'System.Web.UI.WebControls.CheckBoxList' does not contain a public definition for 'GetEnumerator'
Mohd. Mukhtar 22-Nov-12 6:03am    
1. insted of Convert.ToString(GridView1.DataKeys[e.RowIndex].Values["Property_ID"]);
you can use bound field to get Property_ID
2. add using System.Data
3.foreach (ListItem li in chkb.Items)
Raj.Rautela 22-Nov-12 6:16am    
sir i got 1 solution soved my prblm by this..but thanks for your help sir.thanks a lot
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
CheckBoxList chkame = (CheckBoxList)e.Row.FindControl("chklamenities");
string[] strSplitArr = ((DataRowView)e.Row.DataItem)["Amenities"].ToString().Split(',');
foreach (ListItem oItem in chkame.Items)
{
for (int i = 0; i < strSplitArr.Length; i++)
{
if (oItem.Value == strSplitArr[i].Trim())
{
oItem.Selected = true;
break;
}
}
}
}
}

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