GridView and DropDownList

时间:2021-07-18 17:10:20
GridView  and  DropDownList
    <form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gv_KeyList" AutoGenerateColumns="False"
Width="99%" onrowdeleting="gv_KeyList_RowDeleting">
<Columns>
<asp:BoundField DataField="text" HeaderText="Friends" />
<asp:TemplateField> <ItemTemplate>
<asp:DropDownList runat="server" ID="DropDownList1" DataSource='<%#DDLBind() %>' DataTextField="text" DataValueField="id"> </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
 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[];
gv_KeyList.DataBind(); DataTable dt = DDLBind(); for (int i = ; i < gv_KeyList.Rows.Count;i++ )
{
DataRowView mygv=ds.Tables[].DefaultView[i];
for(int j=;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[]; } protected void gv_KeyList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ddlValues = ((DropDownList)gv_KeyList.Rows[e.RowIndex].Cells[].FindControl("DropDownList1")).SelectedValue;
Response.Write(ddlValues);
}
}

三层的前台代码:

GridView  and  DropDownListGridView  and  DropDownList
    <asp:GridView runat="server" ID="gv_KeyList" Width="99%"
AutoGenerateColumns="False" onrowdeleting="gv_KeyList_RowDeleting"
DataKeyNames="I_KeyId" onrowcancelingedit="gv_KeyList_RowCancelingEdit"
onrowediting="gv_KeyList_RowEditing" onrowupdating="gv_KeyList_RowUpdating">
<Columns>
<asp:BoundField DataField="Vc_KeyName" HeaderText="关键词" />
<asp:TemplateField HeaderText="父关键词"> <ItemTemplate>
<asp:DropDownList runat="server" ID="ddlPKey" DataSource='<%#ddlBind() %>' DataTextField="Vc_KeyName" DataValueField="I_KeyId"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Vc_Url" HeaderText="关键词连接" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>

三层UI层代码:

GridView  and  DropDownListGridView  and  DropDownList
 chinaOfQiHuo.BLL.Guanjiancixinxi bllKey = new chinaOfQiHuo.BLL.Guanjiancixinxi();

         protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DDL_PKey.DataTextField = "Vc_KeyName";
DDL_PKey.DataValueField = "I_KeyId";
DDL_PKey.DataSource = ddlBind();
DDL_PKey.DataBind();
gv_KeyList.DataSource = gvBind();
gv_KeyList.DataBind();
//绑定ddl
DropDownList ddl;
for (int i = ; i < gv_KeyList.Rows.Count; i++)
{
for (int j = ; j < ddlBind().Count; j++)
{
if (gvBind()[i].I_SuperId == ddlBind()[j].I_KeyId)
{
ddl = (DropDownList)gv_KeyList.Rows[i].FindControl("ddlPKey");
ddl.SelectedValue = gvBind()[i].I_SuperId.ToString();
}
}
}
}
}
public List<chinaOfQiHuo.Model.Guanjiancixinxi> ddlBind()
{ return bllKey.GetDDLPKey(); }
public List<chinaOfQiHuo.Model.Guanjiancixinxi> gvBind()
{
return bllKey.GetGVList();

三层BLL层代码:

GridView  and  DropDownListGridView  and  DropDownList
  chinaOfQiHuo.DAL.Guanjiancixinxi dalKey = new DAL.Guanjiancixinxi();
public bool Add(chinaOfQiHuo.Model.Guanjiancixinxi model,out string Msg)
{
if (Exists(model.Vc_KeyName))//已存在关键词
{
Msg = "关键词已存在";
return false;
}
else
{
int id = ; id = dalKey.Add(model);
if (id > )
{
Msg = "关键词添加成功";
return true;
}
else {
Msg = "添加超时,请重新添加";
return false;
}
} }
public bool Exists(string keyName)
{
return dal.Exists(keyName);
}
public List<Model.Guanjiancixinxi> GetDDLPKey()
{
return dalKey.GetDDLPKey();
}
public List<Model.Guanjiancixinxi> GetGVList()
{ return dalKey.GetGVKeyList();
} }

三层DAL层代码:

GridView  and  DropDownListGridView  and  DropDownList
  public bool Exists(string keyName)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) from tb_Guanjiancixinxi");
strSql.Append(" where Vc_KeyName = @keyName");
SqlParameter[] parameters = {
new SqlParameter("@keyName", SqlDbType.VarChar,)
};
parameters[].Value = keyName; return DbHelperSQL.Exists(strSql.ToString(), parameters);
}
//绑定父关键字
public List<Model.Guanjiancixinxi> GetDDLPKey()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from tb_Guanjiancixinxi where I_SuperId=0 "); List<Model.Guanjiancixinxi> pKeylist = new List<Model.Guanjiancixinxi>();
SqlDataReader dr= SqlHelper.ExecuteReader(SqlHelper.SqlConnectionString,CommandType.Text,strSql.ToString(),null);
while (dr.Read())
{
Model.Guanjiancixinxi model = new Model.Guanjiancixinxi();
model.Vc_KeyName = dr["Vc_KeyName"].ToString();
model.I_KeyId = Convert.ToInt32(dr["I_KeyId"]);
pKeylist.Add(model);
}
return pKeylist;
}
//获取关键字列表
public List<Model.Guanjiancixinxi> GetGVKeyList()
{
string strSql = "select I_KeyId,I_SuperId,Vc_KeyName,Vc_Url FROM tb_Guanjiancixinxi where I_SuperId!=0";
SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.SqlConnectionString,CommandType.Text,strSql,null);
List<Model.Guanjiancixinxi> gvList = new List<Model.Guanjiancixinxi>();
while (dr.Read())
{
Model.Guanjiancixinxi model = new Model.Guanjiancixinxi();
model.Vc_KeyName = dr["Vc_KeyName"].ToString();
model.PKeyName = GetPKeyName(Convert.ToInt32(dr["I_SuperId"]));
model.I_SuperId = Convert.ToInt32(dr["I_SuperId"]);
model.I_KeyId = Convert.ToInt16(dr["I_KeyId"]);
model.Vc_Url = dr["Vc_Url"].ToString();
gvList.Add(model);
}
return gvList;
}
private string GetPKeyName(int superId)
{
string strSql = "select Vc_KeyName from tb_Guanjiancixinxi where I_KeyId =@superId";
SqlParameter[] spt = new SqlParameter[] { new SqlParameter("@superId", SqlDbType.Int) { Value = superId } };
object obj = SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, strSql,spt);
return obj.ToString();
}
}

GridView与DropDownList的混合使用