gridview编辑时绑定dropdownlist
我想在编辑gridview里的数据的时候绑定dropdownlist,而dropdownlist是动态绑定数据库的,请问代码怎么写呀?急... --------------------编程问答-------------------- 绑数据是绑数据、--------------------编程问答-------------------- 参见
GridView 插入、删除、修改、分页的综合例子
--------------------编程问答-------------------- 刚刚写了个,我也有问题想问,没分了,攒取点分
create table countryTable(
countryID int not null,
countryName nvarchar(30) not null
)
insert into countryTable
select 1,N'中国'
union all
select 2,N'美国'
create table cityTable(
cityID int not null,
countryID int not null,
cityName nvarchar(30) not null
)
insert into cityTable
select 1,1,N'北京'
union all
select 2,1,N'上海'
union all
select 3,2,N'纽约'
union all
select 4,2,N'华盛顿'
<asp:GridView ID="gdv_GridView" runat='server' AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Width="100%"
enablecaching="false" AllowSortTemp="True" ChangedByPageModel="False"
PageSize="10" OnRowDataBound="gdv_GridView_RowDataBound" >
<Columns>
<asp:BoundField DataField="countryID" HeaderText="id" />
<asp:BoundField DataField="countryName" HeaderText="Name" />
<asp:TemplateField headerText="dropdownlist" >
<ItemTemplate>
<asp:DropDownList ID="ddl_dropdownlist" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
gdv_GridView.DataSource = getGdvData();
gdv_GridView.DataBind();
}
protected void gdv_GridView_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = e.Row;
string countryID = row.Cells[0].Text;
DropDownList ddl = (DropDownList)row.Cells[1].FindControl("ddl_dropdownlist");
ddl.DataSource = getCity(countryID);
ddl.DataTextField = "cityName";
ddl.DataValueField = "cityID";
ddl.DataBind();
}
}
private DataTable getCity(string countryID)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(@"");
SqlCommand cmd = new SqlCommand("select cityID,cityName from cityTable where countryID = '" + countryID + "'", conn);
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "city");
dt = ds.Tables["city"];
}
catch
{
}
finally
{
if (conn != null)
conn.Close();
}
return dt;
}
private DataTable getGdvData()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(@"");
SqlCommand cmd = new SqlCommand("select countryID,countryName from countryTable", conn);
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "cnt");
dt = ds.Tables["cnt"];
}
catch
{
}
finally
{
if (conn != null)
conn.Close();
}
return dt;
}
补充:.NET技术 , ASP.NET