2011-05-25 61 views
2

嗨 我有我在asp.net C#网格具有从数据源,数据填充填充 现在网格有一些列的,有一列,它具有1的值,2或3来自数据库,我想,如果它是1,那么它应该显示FSL而不是1,如果它是2然后BTD等,我怎么可以申请检查从C#或ASP,请帮助。如果在asp.net数据网格与其他数据源

+1

你为什么不从查询申请获得FSL 1等等? – 2011-05-25 06:56:48

+1

如何在查询中进行相同的转换,您使用的是什么db? – V4Vendetta 2011-05-25 06:57:04

回答

2

做一个模板列,并放在一个标签:

<asp:TemplateField HeaderText="HeaderText"> 
    <ItemTemplate> 
    <asp:Label ID="lbl" runat="server" ></asp:Label> 
    </ItemTemplate> 

然后你得到它在RowDataBound事件在GridView的:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     DataRow dr = ((DataRowView)e.Row.DataItem).Row; 
     if(dr["ColumnName"].ToString() == "1") 
     { 
      ((Label)e.Row.FindControl("lbl")).Text = "FSL"; 
     } 
     else if(dr["ColumnName"].ToString() == "2") 
     { 
      ((Label)e.Row.FindControl("lbl")).Text = "BTD"; 
     } 
    } 
} 
1

如果你想要得到它查询你可以做(​​对于sql server)

SELECT MyValue = CASE 
      WHEN Col = 1 THEN 'FSL' 
       WHEN Col = 2 THEN 'BTD' 
      ELSE 'Unknown' 
      END 
    FROM MyTable 
1

你也可以用bel嗷嗷提到的方式

<asp:TemplateField HeaderText="HeaderText"> 
    <ItemTemplate> 
    <asp:Label ID="Lbl" runat="server" Text='<%# Convert.ToString(Eval("MyValue"))== "1" ? "FSL" : Convert.ToString(Eval("MyValue")) == "2" ? "BTD" : "Unknown" %>'></asp:Label> 
    </ItemTemplate> 
</asp:TemplateField> 

希望这将有助于你