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
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);
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
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");
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
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
<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>