<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="au_id"> <Columns> <asp:BoundColumn DataField="au_id" HeaderText="au_id" /> <asp:TemplateColumn HeaderText="state"> <ItemTemplate> <asp:DropDownList id="DropDownList1" runat="server" DataSource='<%# GetStates() %>' DataTextField="state" Text='<%# DataBinder.Eval(Container.DataItem,"state") %>'></asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid>
private void BindGrid() { SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); SqlDataAdapter da = new SqlDataAdapter("select au_id, state from authors", cn); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); cn.Close(); DataGrid1.DataSource = ds; DataGrid1.DataKeyField = "au_id"; DataGrid1.DataBind(); } protected DataTable GetStates() { SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); SqlDataAdapter da = new SqlDataAdapter("select distinct state from authors", cn); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); cn.Close(); return ds.Tables[0]; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } }
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="au_id" OnItemDataBound="DataGrid1_ItemDataBound"> <Columns> <asp:BoundColumn DataField="au_id" HeaderText="au_id" /> <asp:TemplateColumn HeaderText="state"> <ItemTemplate> <asp:DropDownList id="DropDownList1" runat="server" DataSource='<%# GetStates() %>' DataTextField="state" Text='<%# DataBinder.Eval(Container.DataItem,"state") %>'></asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="state"> <ItemTemplate> <asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid>
private void BindGrid() { SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); SqlDataAdapter da = new SqlDataAdapter("select au_id, state from authors", cn); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); cn.Close(); DataGrid1.DataSource = ds; DataGrid1.DataKeyField = "au_id"; DataGrid1.DataBind(); } protected DataTable GetStates() { SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); SqlDataAdapter da = new SqlDataAdapter("select distinct state from authors", cn); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); cn.Close(); return ds.Tables[0]; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) { DropDownList dropTemp; string strState; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { strState = ((DataRowView)e.Item.DataItem).Row["state"].ToString(); //对DropDownList做数据绑定 dropTemp = e.Item.FindControl("DropDownList2") as DropDownList; if (dropTemp != null) { SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); string strSql = "select distinct state from authors"; SqlCommand cmd = new SqlCommand(strSql, cn); cn.Open(); dropTemp.DataSource = cmd.ExecuteReader(); dropTemp.DataTextField = "state"; dropTemp.DataBind(); cn.Close(); //到DropDownList中根据type的值去找需要设置为选中状态的项目,将其设置为选中 ListItem item = dropTemp.Items.FindByText(strState); if (item != null) { item.Selected = true; } } } }