如何在GridView中绑定DropDownList
数据库中的表是:s(sno,sname)c(cno,cname)sc(csno,sno,cno,scname)我要在GridView中绑定sc表,在界面中出现(csno,...
数据库中的表是:
s(sno,sname)
c(cno,cname)
sc(csno,sno,cno,scname)
我要在GridView中绑定sc表,在界面中出现(csno,sname,cname,scname,编辑),就是当点击编辑的时候就在本页面(sname,cname)用DropDownList做,并且默认选中编辑前的选项
我用的是EditItemTemplate
但是出不来 啊 展开
s(sno,sname)
c(cno,cname)
sc(csno,sno,cno,scname)
我要在GridView中绑定sc表,在界面中出现(csno,sname,cname,scname,编辑),就是当点击编辑的时候就在本页面(sname,cname)用DropDownList做,并且默认选中编辑前的选项
我用的是EditItemTemplate
但是出不来 啊 展开
推荐于2017-10-14 · 知道合伙人互联网行家
关注
展开全部
1、页面代码
<asp:TemplateField HeaderText="等级">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatUserlevel(Eval("User_UserLevel"))%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
//这个label的作用是为了在后台控制 当你点击编辑按钮的时 DropDownList 的选中值, <asp:Label ID="Label7" runat="server" Text='<%# Eval("User_UserLevel")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddl_userLevel" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
将此列设为模版,在现实数据的时候是以label形式,当点击控件自带的编辑按钮的时候就是以DropDownList形势显示
2、后台代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SystemMemberLevel bll = new SystemMemberLevel();
IList<SystemMemberLevelModel> list = bll.GetAllList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = ((DropDownList)e.Row.FindControl("ddl_userLevel"));
Label lb = ((Label)e.Row.FindControl("Label7"));
if (ddl != null)
{
ddl.DataSource = list;
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.SelectedValue = lb.Text;
ddl.DataBind();
}
}
}
<asp:TemplateField HeaderText="等级">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatUserlevel(Eval("User_UserLevel"))%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
//这个label的作用是为了在后台控制 当你点击编辑按钮的时 DropDownList 的选中值, <asp:Label ID="Label7" runat="server" Text='<%# Eval("User_UserLevel")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddl_userLevel" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
将此列设为模版,在现实数据的时候是以label形式,当点击控件自带的编辑按钮的时候就是以DropDownList形势显示
2、后台代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SystemMemberLevel bll = new SystemMemberLevel();
IList<SystemMemberLevelModel> list = bll.GetAllList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = ((DropDownList)e.Row.FindControl("ddl_userLevel"));
Label lb = ((Label)e.Row.FindControl("Label7"));
if (ddl != null)
{
ddl.DataSource = list;
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.SelectedValue = lb.Text;
ddl.DataBind();
}
}
}
展开全部
得用ItemDataBound这个事件
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
SqlConnection conn=new SqlConnection();
conn.ConnectionString="data source=10.102.7.100;initial catalog="+Session["ufAccount"].ToString()+";persist security info=False;user id=sa;password=tclznmaster;packet size=4096";
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select ccode,(ccode+'|'+ccode_name) as ccode_name from code where charindex('4105',ccode)=1 or patindex('55[01,02]%',ccode)=1 order by ccode",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
DropDownList ddl=(DropDownList)e.Item.FindControls("Ddlcode");
ddl.DataSource=ds.Tables["table1"];
ddl.DataTextField="ccode_name";
ddl.DataValueField="ccode";
ddl.DataBind();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
SqlConnection conn=new SqlConnection();
conn.ConnectionString="data source=10.102.7.100;initial catalog="+Session["ufAccount"].ToString()+";persist security info=False;user id=sa;password=tclznmaster;packet size=4096";
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select ccode,(ccode+'|'+ccode_name) as ccode_name from code where charindex('4105',ccode)=1 or patindex('55[01,02]%',ccode)=1 order by ccode",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
DropDownList ddl=(DropDownList)e.Item.FindControls("Ddlcode");
ddl.DataSource=ds.Tables["table1"];
ddl.DataTextField="ccode_name";
ddl.DataValueField="ccode";
ddl.DataBind();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<asp:DropDownList ID="DropDownList_s" runat="server" DataSourceID="ObjectDatasource1"
DataTextField="sname" DataValueField="sno" SelectedValue='<%# Bind("sno") %>'>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList_c" runat="server" DataSourceID="ObjectDatasource1"
DataTextField="cname" DataValueField="cno" SelectedValue='<%# Bind("cno") %>'>
</asp:DropDownList>
DataTextField="sname" DataValueField="sno" SelectedValue='<%# Bind("sno") %>'>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList_c" runat="server" DataSourceID="ObjectDatasource1"
DataTextField="cname" DataValueField="cno" SelectedValue='<%# Bind("cno") %>'>
</asp:DropDownList>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SingleLogin.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>GridView动态添加模板列的例子</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="标题" DataField="text"/>
</Columns>
</asp:GridView>
</form>
</body>
</html>
/*********************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
dt.Columns.Add(new DataColumn("text", typeof(string)));
dt.Columns.Add(new DataColumn("remark",typeof(string)));
for (int i = 0; i < 6; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "列表项目 " + i.ToString();
dr[2] = "第" + i.ToString() + "行";
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
DropDownList drr = new DropDownList();
drr.ID = "dropdown";
drr.AppendDataBoundItems = true;
drr.Items.Add(new ListItem("-----请选择------", ""));
drr.Items.Add(new ListItem("AA", "a"));
drr.Items.Add(new ListItem("BB", "b"));
drr.Items.Add(new ListItem("CC", "c"));
container.Controls.Add(drr);
break;
default:
break;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
GridView1.Columns.Add(customField);
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
//下面只是例子,举一反三,不再费话了
DataRowView gv = (DataRowView)e.Row.DataItem;
int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
DropDownList dr = (DropDownList)e.Row.FindControl("dropdown");
dr.SelectedIndex = itemSeleted;
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>GridView动态添加模板列的例子</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="标题" DataField="text"/>
</Columns>
</asp:GridView>
</form>
</body>
</html>
/*********************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
dt.Columns.Add(new DataColumn("text", typeof(string)));
dt.Columns.Add(new DataColumn("remark",typeof(string)));
for (int i = 0; i < 6; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "列表项目 " + i.ToString();
dr[2] = "第" + i.ToString() + "行";
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
DropDownList drr = new DropDownList();
drr.ID = "dropdown";
drr.AppendDataBoundItems = true;
drr.Items.Add(new ListItem("-----请选择------", ""));
drr.Items.Add(new ListItem("AA", "a"));
drr.Items.Add(new ListItem("BB", "b"));
drr.Items.Add(new ListItem("CC", "c"));
container.Controls.Add(drr);
break;
default:
break;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
GridView1.Columns.Add(customField);
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
//下面只是例子,举一反三,不再费话了
DataRowView gv = (DataRowView)e.Row.DataItem;
int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
DropDownList dr = (DropDownList)e.Row.FindControl("dropdown");
dr.SelectedIndex = itemSeleted;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询