使用DataGrid把数据库里的数据都显示出来。
如果某一字段我要根据里面的值来显示不同的内容要怎么办?
例如:表中有一个表示状态的字段,如果这个字段的值为1,我要在页面上显示“完成”,如果为2,要显示“等待”,要怎么绑定啊DataGrid?html:<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="True" Width="540px" PageSize="5" AllowSorting="True">
<HeaderStyle BackColor="#409DDE"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="c_busi_desc" HeaderText="分析主题">
<HeaderStyle Width="180px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_billing_month_id" HeaderText="回退帐务月">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_region_id" HeaderText="回退地市">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_start_time" HeaderText="回退开始时间">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_flag" HeaderText="处理状态">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle NextPageText="下一页" PrevPageText="上一页"></PagerStyle>
</asp:DataGrid>绑定: string CnnStr = System.Configuration.ConfigurationSettings.AppSettings["Connectingstring"].ToString();
string Sql = "select * from meta_etl_cancel_configtt order by c_flag Desc,c_start_time Desc";
DataSet ds = SqlHelper.ExecuteDataset(CnnStr,CommandType.Text,Sql);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
我是这样直接绑定的,要实现上面的要求,要怎么做?
如果某一字段我要根据里面的值来显示不同的内容要怎么办?
例如:表中有一个表示状态的字段,如果这个字段的值为1,我要在页面上显示“完成”,如果为2,要显示“等待”,要怎么绑定啊DataGrid?html:<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="True" Width="540px" PageSize="5" AllowSorting="True">
<HeaderStyle BackColor="#409DDE"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="c_busi_desc" HeaderText="分析主题">
<HeaderStyle Width="180px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_billing_month_id" HeaderText="回退帐务月">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_region_id" HeaderText="回退地市">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_start_time" HeaderText="回退开始时间">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_flag" HeaderText="处理状态">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle NextPageText="下一页" PrevPageText="上一页"></PagerStyle>
</asp:DataGrid>绑定: string CnnStr = System.Configuration.ConfigurationSettings.AppSettings["Connectingstring"].ToString();
string Sql = "select * from meta_etl_cancel_configtt order by c_flag Desc,c_start_time Desc";
DataSet ds = SqlHelper.ExecuteDataset(CnnStr,CommandType.Text,Sql);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
我是这样直接绑定的,要实现上面的要求,要怎么做?
<HeaderStyle BackColor="#409DDE"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="c_busi_desc" HeaderText="分析主题">
<HeaderStyle Width="180px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_billing_month_id" HeaderText="回退帐务月">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_region_id" HeaderText="回退地市">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_start_time" HeaderText="回退开始时间">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="处理状态">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.colname").ToString()==1?"正常":"等待" %>
</ItemTemplate>
<asp:TemplateColumn>
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle NextPageText="下一页" PrevPageText="上一页"></PagerStyle>
</asp:DataGrid>
select
case 字段名
when 1
then '完成'
when 2
then '等待'
else
''
end as 字段名
,* from meta_etl_cancel_configtt order by c_flag这样再帮定就可以了
<%# chuli(DataBinder.Eval(Container, "DataItem.colname) %>代码中
public string chuli(string str)
{
if(str.CompareTo("1")==0)
{
return "完成";
}
elseif(str.CompareTo("2")==0)
{
return "等待";
}
else
{
return "";
}
}