如何在GridView中绑定DropDownList
发布网友
发布时间:2022-04-27 09:25
我来回答
共5个回答
懂视网
时间:2022-04-27 13:46
GridView自带的分页功能实现:
要实现GrdView分页的功能
操作如下:
- 更改GrdView控件的AllowPaging属性为true。
- 更改GrdView控件的PageSize属性为 任意数值(默认为10)
- 更改GrdView控件的PageSetting->Mode为Numeric等(默认为Numeric)该属性为分页样式。
GridView属性设置好了,从页面上也能看到分页样式。
现在开始实现分页的功能:
- 在<<asp:GridView ID=......>后添加,OnPageIndexChanging="GridView1_PageIndexChanging"
- 在对应的aspx.cs中添加:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
InitPage(); //重新绑定GridView数据的函数
}
参考代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview_zidaifenye.aspx.cs" Inherits="gridview_zidaifenye" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" PagerSettings-Mode="Numeric"
OnRowDataBound="GridView1_RowDataBound" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Stock" HeaderText="Stock" SortExpression="Stock" />
</Columns>
</asp:GridView>
每页显示<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
</asp:DropDownList>
条记录
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class gridview_zidaifenye : System.Web.UI.Page
{
DBAccess db = new DBAccess();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// gvProduct.DataSource = getData();
// gvProduct.DataBind();
BindGrid();
}
}
public void BindGrid()
{
SqlCommand comm = db.CreateCommand("select * from product p,Uuser u where p.userid=u.id");
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = comm;
DataSet ds = new DataSet();
sda.Fill(ds,"Datatable");
DataView dv = ds.Tables[0].DefaultView;
GridView1.DataSource = dv;
GridView1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageSize = int.Parse(DropDownList1.SelectedValue);
GridView1.PageIndex = 0;
BindGrid(); //GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
lblMsg.Text = "当前页为第" + (GridView1.PageIndex + 1).ToString() + "页,共" + (GridView1.PageCount).ToString() + "页";
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = ((GridViewPageEventArgs)e).NewPageIndex;
BindGrid(); //重新绑定GridView数据的函数
}
}
总结下,方便以后用到。
热心网友
时间:2022-04-27 10:54
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();
}
}
}
热心网友
时间:2022-04-27 12:12
得用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();
}
热心网友
时间:2022-04-27 13:47
<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>
热心网友
时间:2022-04-27 15:38
<%@ 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;
}
}
}