GridView的行数据绑定复选框列表 [英] Gridview rowdatabound for checkboxlist

查看:55
本文介绍了GridView的行数据绑定复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我在gridview编辑项目模板中使用了一个复选框列表.
有一个问题.在编辑模式下,我在edititem模板的复选框列表中没有得到以前检查过的值.我想更新记录.
请帮忙.提前谢谢..

值以这种格式保存在数据库中
健身房,公园,电梯,安全,俱乐部房屋,游泳池,蓄水,备用电源,预留停车位,Vaastu投诉",我想在gridview的编辑命令上将这些值绑定到复选框列表中...


更新代码并查询

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

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

String Ame = "";
                for (int i = 0; i < chklamenities.Items.Count; i++)
                {
                    if (chklamenities.Items[i].Selected)
                    {
                        Ame += chklamenities.Items[i].Value + " ,";
                    }
                }
                Ame = Ame.TrimEnd(',');



这是我的gridview设计



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>

推荐答案

拉吉,

如下更新代码.

Hi Raj,

Update your code as below.

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(",");
      }
   }
}


请参阅下面的行数据绑定方法.

Please see the below row data bound method.

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;        
        }                    
      }
    }
   }
}


这篇关于GridView的行数据绑定复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆