DataGrid中使用的DropDownList控件,怎样使DataGrid绑定的表和DropDownList绑定的不同 本来就不同的,你先把DataGrid邦定一个DataSet然后再由DataGrid的每一行中找到DropDownList控件,再把它跟数据邦定 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this is a simple e.f:you try:1)在该DataGrid添加:OnItemDataBound="Grid_ItemDataBound" 添加模板列中,其中:...<ASP:TEMPLATECOLUMN HeaderText="机构信息"><ITEMTEMPLATE><ASP:DROPDOWNLIST id="DropDownList1" Runat="server"></ASP:DROPDOWNLIST></ITEMTEMPLATE></ASP:TEMPLATECOLUMN>...2)Add EVENT HANDLERS:protected void Grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e){if(e.Item.ItemIndex>=0){DropDownList myDropDownList = (DropDownList)e.Item.FindControl("DropDownList1");//查找数据并绑定到dtSqlConnection oConn = new SqlConnection(m_connString);SqlCommand cmd = new SqlCommand("select mc,id from your table",oConn);oConn.Open();SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);myDropDownList.DataSource = dr ;myDropDownList.SelectedIndex = 0;myDropDownList.DataTextField ="mc";myDropDownList.DataValueField="id";myDropDownList.DataBind();dr.Close();}} 分别绑定就是了,举个例子datagird绑定的是 DS1dropdownlist(DS2)<EditItemTemplate> <asp:DropDownList id=DropDownList1 runat="server" Width="204px" Height="22px" datasource="<%# DS2 %>" DataMember="cities" DataValueField="city" DataTextField="city" AutoPostBack="True" OnSelectedIndexChanged="MyChanged"> </asp:DropDownList></EditItemTemplate> 我的几段代码如下,希望高手帮我看看:Private Sub CreatDataSet() '生成datagird数据连接 MyConn = New OleDbConnection("Provider=MSDAORA.1;Password=test;User ID=test;Data Source=ora9ierp_hj") myAdpter.SelectCommand = New OleDbCommand("select * from W_DEPT_STORY ", MyConn) '取得数据来源中的记录 myAdpter.Fill(mySet, "W_DEPT_STORY") End SubPrivate Sub CreatDataDSet() '生成DropDownList数据连接 MyDConn = New OleDbConnection("Provider=MSDAORA.1;Password=test;User ID=test;Data Source=ora9ierp_hj") myDAdpter.SelectCommand = New OleDbCommand("select * from W_DEPT ", MyDConn) '取得数据来源中的记录 myDAdpter.Fill(myDSet, "W_DEPT") End Sub'添加DropDownList的语句及设置<EditItemTemplate> <asp:DropDownList id=DropDownList1 runat="server" Width="133px" DataSource="<% # myDset %>" DataTextField="DEPT_NAME" DataValueField="DEPT_NAME" DataMember="W_DEPT"></asp:DropDownList>'表格编辑事件Private Sub G_history_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles G_history.EditCommand G_history.EditItemIndex = e.Item.ItemIndex'下句出错,未将对象引用设置到对象的实例。 G_history.DataSource = mySet.Tables("W_DEPT_STORY").DefaultView G_history.DataBind()我这样设置为什么出错呀 End Sub</EditItemTemplate> 在我发给你的例子里面ComboInGrid.aspx里面就有。 不是你的设置出错,是mySet在edit的时候没有实例,这是因为asp.net不会为你保存mySet,你需要将fill后的数据保存在某个地方(Session,或者ViewState). 请教niwalker:在哪个地方保存mySet呢?我在CreatDataSet() 里加了一句Me.Session("MyDataSet") = mySet,然后:Private Sub G_history_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles G_history.CancelCommand G_history.EditItemIndex = -1 mySet = Session("MyDataSet") G_history.DataSource = mySet.Tables("W_DEPT_STORY").DefaultView G_history.DataBind() End Sub怎么提示:IListSource 不包含任何数据源。 ayine(原振侠) :请指教。 Js控制CSS 求解:system 需要更高的orecle版本或者客户端-老问题,新环境 webPart发布之后浏览问题 请问Cookie的存储和读写方式有几种啊 帮忙指导一下 全文检索结果中的关键词如何加上颜色? ASP.NET 中怎样实现一个动态改变的图片文件夹内图片的轮换显示 租用别人的服务器,如果服务器没有装水晶报表,请问我的水晶报表程序可以运行吗? 该死的VSS总出问题,郁闷中,散分! 怎样在声明一个在asp.net工程中让所有叶面都能使用的变量 为什么在vs.net中做的网页在ie中无法预览?并显示如下的错误信息 大家看看这个语句
you try:
1)在该DataGrid添加:OnItemDataBound="Grid_ItemDataBound"
添加模板列中,其中:
...
<ASP:TEMPLATECOLUMN HeaderText="机构信息">
<ITEMTEMPLATE>
<ASP:DROPDOWNLIST id="DropDownList1" Runat="server"></ASP:DROPDOWNLIST>
</ITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>
...2)Add EVENT HANDLERS:
protected void Grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex>=0)
{
DropDownList myDropDownList = (DropDownList)e.Item.FindControl("DropDownList1");
//查找数据并绑定到dt
SqlConnection oConn = new SqlConnection(m_connString);
SqlCommand cmd = new SqlCommand("select mc,id from your table",oConn);
oConn.Open();SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
myDropDownList.DataSource = dr ;
myDropDownList.SelectedIndex = 0;
myDropDownList.DataTextField ="mc";
myDropDownList.DataValueField="id";
myDropDownList.DataBind();
dr.Close();
}
}
datagird绑定的是 DS1dropdownlist(DS2)<EditItemTemplate>
<asp:DropDownList id=DropDownList1 runat="server" Width="204px" Height="22px" datasource="<%# DS2 %>" DataMember="cities" DataValueField="city" DataTextField="city" AutoPostBack="True" OnSelectedIndexChanged="MyChanged">
</asp:DropDownList>
</EditItemTemplate>
MyConn = New OleDbConnection("Provider=MSDAORA.1;Password=test;User ID=test;Data Source=ora9ierp_hj")
myAdpter.SelectCommand = New OleDbCommand("select * from W_DEPT_STORY ", MyConn) '取得数据来源中的记录
myAdpter.Fill(mySet, "W_DEPT_STORY")
End SubPrivate Sub CreatDataDSet() '生成DropDownList数据连接
MyDConn = New OleDbConnection("Provider=MSDAORA.1;Password=test;User ID=test;Data Source=ora9ierp_hj")
myDAdpter.SelectCommand = New OleDbCommand("select * from W_DEPT ", MyDConn) '取得数据来源中的记录
myDAdpter.Fill(myDSet, "W_DEPT")
End Sub'添加DropDownList的语句及设置
<EditItemTemplate>
<asp:DropDownList id=DropDownList1 runat="server" Width="133px" DataSource="<% # myDset %>" DataTextField="DEPT_NAME" DataValueField="DEPT_NAME" DataMember="W_DEPT"></asp:DropDownList>'表格编辑事件
Private Sub G_history_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles G_history.EditCommand
G_history.EditItemIndex = e.Item.ItemIndex
'下句出错,未将对象引用设置到对象的实例。
G_history.DataSource = mySet.Tables("W_DEPT_STORY").DefaultView
G_history.DataBind()我这样设置为什么出错呀 End Sub
</EditItemTemplate>
在哪个地方保存mySet呢?我在CreatDataSet() 里加了一句Me.Session("MyDataSet") = mySet,然后:
Private Sub G_history_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles G_history.CancelCommand
G_history.EditItemIndex = -1
mySet = Session("MyDataSet")
G_history.DataSource = mySet.Tables("W_DEPT_STORY").DefaultView
G_history.DataBind()
End Sub怎么提示:
IListSource 不包含任何数据源。
请指教。