DataGrid中放DropDownList的问题
大家好,小弟今天在写程序时碰到了一个问题,想向大家请问一下,问题是这样的,我在DataGrid中放了个一模板列,然后在模板列的项模板中放了一个DropDownList想根据数据库中的roleid这个字段的值来显示DropDownList的文本,当roleid的值为1时显示通过,当roleid的值为0时显示未通过.请问该怎么设置DataGrid和DropDownList才能实现啊?先在此谢谢各位了.
[解决办法]
ItemDataBound事件中设置
[解决办法]
//aspx
<asp:DataGrid ID= "DataGrid1 " runat= "server " AutoGenerateColumns= "False " >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID= "Label1 " runat= "server " Text= ' <%# DataBinder.Eval(Container.DataItem, "au_id " ) %> '> </asp:Label>
<asp:DropDownList ID= "DropDownList1 " runat= "server ">
<asp:ListItem Value= "1 "> 通过 </asp:ListItem>
<asp:ListItem Value= "0 "> 未通过 </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn> </Columns>
</asp:DataGrid>
//aspx.cs
private void BindGrid()
{
SqlConnection cn = new SqlConnection(@ "server=.\SQLExpress;uid=sa;pwd=password;database=pubs ");
SqlDataAdapter da = new SqlDataAdapter( "select * from authors ", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
foreach (DataGridItem dgi in DataGrid1.Items)
{
DropDownList d = (DropDownList)dgi.FindControl( "DropDownList1 ");
if (d != null)
{
bool b = (bool)ds.Tables[0].Rows[dgi.ItemIndex][ "contract "];
if (b)
d.SelectedIndex = 0;
else
d.SelectedIndex = 1;
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
[解决办法]
感觉数据库中写方便。
select case roleid when 1 then '通过 '
else '未通过 ' end
as ttt
from ......