在DataGrid 中的 DropDownList要从数据表1中绑定一组数据,并对应数据库2中的某个字段值
例如表1 表2
id1 title1 id2 title2
1 a 1 a
2 b 2 b
3 c 3 a
.. ..DropDownList 从表1中 绑定title1的值
浏览时还要默认指向title2的那一项十分火急,各位帮忙!!!!!!
例如表1 表2
id1 title1 id2 title2
1 a 1 a
2 b 2 b
3 c 3 a
.. ..DropDownList 从表1中 绑定title1的值
浏览时还要默认指向title2的那一项十分火急,各位帮忙!!!!!!
DropDownList1.DataMember=b.title2
DropDownList1.DataValueField=a.title1
我手动写了DropDownList显示的数据(不从库里绑定了)
有没有办法让他默认指向title2中的那一项呀
<Columns>
<asp:TemplateColumn HeaderText="state">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"state")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="DropDownList1" runat="server" Enabled="true"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlDataAdapter da = new SqlDataAdapter("select au_id, state, contract from authors", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataKeyField = "au_id";
DataGrid1.DataBind();
} private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList dropTemp;
string strState;
if(e.Item.ItemType == ListItemType.EditItem || e.Item.ItemType == ListItemType.AlternatingItem)
{
strState = ((DataRowView)e.Item.DataItem).Row["state"].ToString(); //对DropDownList做数据绑定
dropTemp = (DropDownList)e.Item.Cells[3].FindControl("DropDownList1");
if(dropTemp != null)
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
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;
}
}
}
}
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="delete"></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="state">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"state")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="DropDownList1" runat="server" Enabled="true"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></form>
//aspx.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;public class WebFormTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1; private void BindGrid()
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
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();
} private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
} private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindGrid();
} private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//删除代码
DataGrid1.EditItemIndex = -1;
BindGrid();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//更新代码
DataGrid1.EditItemIndex = -1;
BindGrid();
} private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList dropTemp;
string strState;
if(e.Item.ItemType == ListItemType.EditItem || e.Item.ItemType == ListItemType.AlternatingItem)
{
strState = ((DataRowView)e.Item.DataItem).Row["state"].ToString(); //对DropDownList做数据绑定
dropTemp = (DropDownList)e.Item.Cells[2].FindControl("DropDownList1");
if(dropTemp != null)
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
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;
}
}
}
}
}