.aspx
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 96px" runat="server" OnItemDataBound="Item_Bind">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<FONT face="宋体">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList></FONT>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace test6
{
/// <summary>
/// WebForm4 的摘要说明。
/// </summary>
public class WebForm4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string connstr="server=localhost;uid=sa;password=shining;database=xhotel";
SqlConnection conn=new SqlConnection(connstr);
string selstr="select * from t_q_cod";
SqlDataAdapter da=new SqlDataAdapter(selstr,conn);
DataSet ds=new DataSet();
da.Fill(ds); DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
public void Item_Bind(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex>=0)
{
string []dl_value=new string[8];
for(int i=0;i<8;i++)
{
dl_value[i]=i.ToString();
}
((DropDownList)e.Item.FindControl("DropDownList1")).DataSource=dl_value;
((DropDownList)e.Item.FindControl("DropDownList1")).DataBind();
}
}
#region Web Form Designer generated code override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}要查用数据库的值绑定你只要把 dl_value改为你从数据库中取出得就行了。
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 96px" runat="server" OnItemDataBound="Item_Bind">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<FONT face="宋体">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList></FONT>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace test6
{
/// <summary>
/// WebForm4 的摘要说明。
/// </summary>
public class WebForm4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string connstr="server=localhost;uid=sa;password=shining;database=xhotel";
SqlConnection conn=new SqlConnection(connstr);
string selstr="select * from t_q_cod";
SqlDataAdapter da=new SqlDataAdapter(selstr,conn);
DataSet ds=new DataSet();
da.Fill(ds); DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
public void Item_Bind(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex>=0)
{
string []dl_value=new string[8];
for(int i=0;i<8;i++)
{
dl_value[i]=i.ToString();
}
((DropDownList)e.Item.FindControl("DropDownList1")).DataSource=dl_value;
((DropDownList)e.Item.FindControl("DropDownList1")).DataBind();
}
}
#region Web Form Designer generated code override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}要查用数据库的值绑定你只要把 dl_value改为你从数据库中取出得就行了。
解决方案 »
- 请专家看看,为什么会有web.config为什么会有这种连接字符串?
- 请教用C++写.NET!(帮顶有分)
- aspx的算法是在哪里?
- 指定框架里显示aspx页面的问题
- 找不到类型或命名空间名称“UserControlCS”?(问题放了几天了,高手们)
- 讨论个关于HtmlInputText控件ID的问题,参与有分
- 高分求c#中的不对称加密解密的说明 谢谢分不够再加
- Asp.net(C#) 调用存储过程输出参数的值
- 关于csc命令的问题
- 一个有Ie的问题
- 一个asp.net的大问题,希望是我编程的问题,不是asp.net的bug,高手们请进!!
- 在从池中获取连接之前超时时间已过。出现这种情况可能是因为所有池连接都已被使用并已达到最大池大小。怎么解决
e.Item.Cells[1].Text.Trim()就是你要的数值
可以根据这来查找数据库并绑定到你的dropdownlist中
若是前者,你的dropdownlist的数据可以在page_load的时候提取一次,在每个dg的itembound的时候,再设定itemselectitem的value或text。
若是后者,只是在edit的时候帮定即可。令:dropdownlist的帮定和dg一样,用datasource指定。在分别指定datatextfield和datavaluefield。
如何为该孔件绑定数据呢?我试过在page_load 里使用了一个
dataset.tables("newtable"),想让dropdownlist绑定这个table
但是一直实现不了,希望哪位高手能提示我一下!万分感谢!