gridview问题
本人才做.net不久,gridview显示数据时,比如某一列的值是001(编号),看起来不方便,想做个dropdownlist来显示具体的含义,该怎么做?以及如何取这个值.多谢
------解决方案--------------------,比如某一列的值是001(编号),看起来不方便
----
那你就不绑定这一列
你想用那个dlst怎样来表示具体的含义?不明白你的意思
------解决方案--------------------把编号和对应的信息建个表,用楼上的语句就可以了。
------解决方案--------------------用Join和隐藏列不就完了么
------解决方案--------------------Select 编号B,名称,编号A, 名称A from A, B
Where A.编号A = B.编号A
然后隐藏编号A列就可以了。
------解决方案--------------------//aspx
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "GridView1 " runat= "server " DataKeyNames= "au_id " AutoGenerateColumns= "False " OnRowDataBound= "GridView1_RowDataBound " >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList id= "dropTemp " runat= "server "> </asp:DropDownList>
<asp:Label ID= "Label1 " runat= "server " Text= ' <%# Eval( "title_id ") %> '> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
//aspx.cs
private void BindGrid()
{
SqlConnection cn = new SqlConnection(@ "server=.\SQLExpress;uid=sa;pwd=password;database=pubs ");
string strSQL = "select au_id,title_id from titleauthor ";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
cn.Close();
}
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
BindGrid();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//保存当前行的au_id的值
string au_id = this.GridView1.DataKeys[e.Row.RowIndex][ "au_id "].ToString();
//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl( "dropTemp ");
SqlConnection cn = new SqlConnection(@ "server=.\SQLExpress;uid=sa;pwd=password;database=pubs ");
string strSQL = "select au_id from authors ";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
dropTemp.DataSource = cmd.ExecuteReader();
dropTemp.DataTextField = "au_id ";
dropTemp.DataBind();
//到DropDownList中根据au_id的值去找需要设置为选中状态的项目,将其设置为选中
ListItem item = dropTemp.Items.FindByText(au_id);
if(item != null)
{
item.Selected = true;
}
cn.Close();
}
}