GridView and DropDownList

 1    <form id="form1" runat="server">
 2     <div>
 3     <asp:GridView runat="server" ID="gv_KeyList" AutoGenerateColumns="False" 
 4             Width="99%" onrowdeleting="gv_KeyList_RowDeleting">
 5         <Columns>
 6             <asp:BoundField DataField="text" HeaderText="Friends" />
 7             <asp:TemplateField>
 8                
 9                 <ItemTemplate>
10                    <asp:DropDownList runat="server" ID="DropDownList1"  DataSource='<%#DDLBind() %>' DataTextField="text" DataValueField="id"> </asp:DropDownList>
11                 </ItemTemplate>
12             </asp:TemplateField>
13             <asp:CommandField ShowDeleteButton="True" />
14         </Columns>
15         </asp:GridView>
16     </div>
17     </form>
View Code
 if (!IsPostBack)
        {
            DropDownList dll;
            string strSql = "select * from emailreceive where p_id!=1 ";
            string conn = "server=.;database=emailfriends;uid=sa;pwd=123";
            SqlConnection conns = new SqlConnection(conn);
            conns.Open();
            SqlCommand cmd = new SqlCommand(strSql, conns);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, "a");
            gv_KeyList.DataSource = ds.Tables[0];
            gv_KeyList.DataBind();
         
            DataTable dt = DDLBind();
        
            for (int i = 0; i < gv_KeyList.Rows.Count;i++ )
            { 
                DataRowView mygv=ds.Tables[0].DefaultView[i];
               for(int j=0;j<dt.Rows.Count;j++)
               {
               
                   DataRowView pKeyName = dt.DefaultView[j];
                   if (Convert.ToInt32(mygv["p_id"]) == Convert.ToInt32(pKeyName["id"]))
                   {
                       dll = (DropDownList)gv_KeyList.Rows[i].FindControl("DropDownList1");
                       string id=mygv["p_id"].ToString();
                       dll.SelectedValue = id;
                      }
               }
              

            
            }     
                      
              
                conns.Close();
         
        }
    }

    public DataTable DDLBind()
    {
        string strSql1 = "select * from emailreceive where p_id=1 ";
        string conn1 = "server=.;database=emailfriends;uid=sa;pwd=123";
        SqlConnection conns1 = new SqlConnection(conn1);
        conns1.Open();
        SqlCommand cmd1 = new SqlCommand(strSql1, conns1);
        SqlDataAdapter sda1 = new SqlDataAdapter();
        sda1.SelectCommand = cmd1;
        DataSet ds1 = new DataSet();
        sda1.Fill(ds1, "a");
        conns1.Close();
        return ds1.Tables[0];
    
    }

    protected void gv_KeyList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ddlValues = ((DropDownList)gv_KeyList.Rows[e.RowIndex].Cells[1].FindControl("DropDownList1")).SelectedValue;
        Response.Write(ddlValues);
    }
}

三层的前台代码:

 1    <asp:GridView runat="server" ID="gv_KeyList" Width="99%" 
 2             AutoGenerateColumns="False" onrowdeleting="gv_KeyList_RowDeleting" 
 3             DataKeyNames="I_KeyId" onrowcancelingedit="gv_KeyList_RowCancelingEdit" 
 4             onrowediting="gv_KeyList_RowEditing" onrowupdating="gv_KeyList_RowUpdating">
 5         <Columns>
 6             <asp:BoundField DataField="Vc_KeyName" HeaderText="关键词" />
 7             <asp:TemplateField HeaderText="父关键词">
 8                
 9                 <ItemTemplate>
10                    <asp:DropDownList runat="server" ID="ddlPKey" DataSource='<%#ddlBind() %>' DataTextField="Vc_KeyName" DataValueField="I_KeyId"></asp:DropDownList>
11                 </ItemTemplate>
12             </asp:TemplateField>
13             <asp:BoundField DataField="Vc_Url" HeaderText="关键词连接" />
14             <asp:CommandField HeaderText="修改" ShowEditButton="True" />
15             <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
16         </Columns>
17         </asp:GridView>
View Code

三层UI层代码:

 1 chinaOfQiHuo.BLL.Guanjiancixinxi bllKey = new chinaOfQiHuo.BLL.Guanjiancixinxi();
 2 
 3         protected void Page_Load(object sender, EventArgs e)
 4         {
 5             if (!IsPostBack)
 6             {
 7                 DDL_PKey.DataTextField = "Vc_KeyName";
 8                 DDL_PKey.DataValueField = "I_KeyId";
 9                 DDL_PKey.DataSource = ddlBind();
10                 DDL_PKey.DataBind();
11                  gv_KeyList.DataSource = gvBind();
12                  gv_KeyList.DataBind();
13                 //绑定ddl
14                 DropDownList ddl;
15                 for (int i = 0; i < gv_KeyList.Rows.Count; i++)
16                 {
17                     for (int j = 0; j < ddlBind().Count; j++)
18                     {
19                         if (gvBind()[i].I_SuperId == ddlBind()[j].I_KeyId)
20                         {
21                             ddl = (DropDownList)gv_KeyList.Rows[i].FindControl("ddlPKey");
22                             ddl.SelectedValue = gvBind()[i].I_SuperId.ToString();
23                         }
24                     }
25                 }
26             }
27         }
28         public  List<chinaOfQiHuo.Model.Guanjiancixinxi> ddlBind()
29         {
30 
31             return bllKey.GetDDLPKey();
32         
33         }
34         public List<chinaOfQiHuo.Model.Guanjiancixinxi> gvBind()
35         {
36           return  bllKey.GetGVList();
37         
View Code

三层BLL层代码:

 1  chinaOfQiHuo.DAL.Guanjiancixinxi dalKey = new DAL.Guanjiancixinxi();
 2        public bool Add(chinaOfQiHuo.Model.Guanjiancixinxi model,out string Msg)
 3        {
 4            if (Exists(model.Vc_KeyName))//已存在关键词
 5            {
 6                Msg = "关键词已存在";
 7                return false;
 8            }
 9            else
10            {
11                int id = 0;
12 
13                id = dalKey.Add(model);
14                if (id > 0)
15                {
16                    Msg = "关键词添加成功";
17                    return true;
18                }
19                else {
20                    Msg = "添加超时,请重新添加";
21                    return false;
22                }
23            }
24        
25        }
26        public bool Exists(string keyName)
27        {
28            return dal.Exists(keyName);
29        }
30        public List<Model.Guanjiancixinxi> GetDDLPKey()
31        {
32            return dalKey.GetDDLPKey();
33        }
34        public List<Model.Guanjiancixinxi> GetGVList()
35        {
36        
37          return  dalKey.GetGVKeyList();
38        }
39      
40     }
View Code

三层DAL层代码:

 1  public bool Exists(string keyName)
 2        {
 3            StringBuilder strSql = new StringBuilder();
 4            strSql.Append("select count(1) from tb_Guanjiancixinxi");
 5            strSql.Append(" where Vc_KeyName = @keyName");
 6            SqlParameter[] parameters = {
 7                     new SqlParameter("@keyName", SqlDbType.VarChar,50)
 8             };
 9            parameters[0].Value = keyName;
10 
11            return DbHelperSQL.Exists(strSql.ToString(), parameters);
12        }
13        //绑定父关键字
14        public List<Model.Guanjiancixinxi> GetDDLPKey()
15        {
16            StringBuilder strSql = new StringBuilder();
17            strSql.Append("select * from tb_Guanjiancixinxi where I_SuperId=0 ");
18           
19            List<Model.Guanjiancixinxi> pKeylist = new List<Model.Guanjiancixinxi>();
20           SqlDataReader dr= SqlHelper.ExecuteReader(SqlHelper.SqlConnectionString,CommandType.Text,strSql.ToString(),null);
21           while (dr.Read())
22           {
23               Model.Guanjiancixinxi model = new Model.Guanjiancixinxi();
24               model.Vc_KeyName = dr["Vc_KeyName"].ToString();
25               model.I_KeyId = Convert.ToInt32(dr["I_KeyId"]);
26               pKeylist.Add(model);
27           }
28           return pKeylist;
29        }
30      //获取关键字列表
31        public List<Model.Guanjiancixinxi> GetGVKeyList()
32        {
33            string strSql = "select I_KeyId,I_SuperId,Vc_KeyName,Vc_Url FROM tb_Guanjiancixinxi where I_SuperId!=0";
34            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.SqlConnectionString,CommandType.Text,strSql,null);
35            List<Model.Guanjiancixinxi> gvList = new List<Model.Guanjiancixinxi>();
36            while (dr.Read())
37            {
38                Model.Guanjiancixinxi model = new Model.Guanjiancixinxi();
39                model.Vc_KeyName = dr["Vc_KeyName"].ToString();
40                model.PKeyName = GetPKeyName(Convert.ToInt32(dr["I_SuperId"]));
41                model.I_SuperId = Convert.ToInt32(dr["I_SuperId"]);
42                model.I_KeyId = Convert.ToInt16(dr["I_KeyId"]);
43                model.Vc_Url = dr["Vc_Url"].ToString();
44                gvList.Add(model);
45            }
46            return gvList;
47        }
48        private string GetPKeyName(int superId)
49        {
50            string strSql = "select Vc_KeyName from tb_Guanjiancixinxi where I_KeyId =@superId";
51            SqlParameter[] spt = new SqlParameter[] { new SqlParameter("@superId", SqlDbType.Int) { Value = superId } };
52            object obj = SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, strSql,spt);
53            return obj.ToString();
54        }
55     }
View Code

GridView与DropDownList的混合使用

posted @ 2013-10-21 22:59  米兰_跳跳虎  阅读(236)  评论(0编辑  收藏  举报