2013-08-30 43 views
0

我正在使用模板字段來顯示基於表字段中的條件的文本。如何根據數據庫中的列值顯示文本?

我有字段名資格我用來存儲整數值,例如4或3 如果是4,然後在GridView的

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
    <asp:Label ID="Label1" runat="server" Text='<%# GetQual(int)(Eval("Qualification")) %>'></asp:Label> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 

public static string GetQual(int value) 
    { 
     if (value == 4) 
     { 
      return "Post Graduate"; 
     } 
     else 
     { 
      return "Graduate"; 

     } 
    } 

回答

0

顯示「研究生」其他「畢業生」我會用RowDataBound,因爲它是更具可讀性和可維護性:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     DataRow row = ((DataRowView)e.Row.DataItem).Row; // dependas on the DataSource, maybe you need to use the debugger to see what it is 
     int qualification = row.Field<int>("Qualification"); 
     Label lblQuali = (Label) e.Row.FindControl("Label1"); 
     lblQuali.Text = qualification == 4 ? "Post Graduate" : "Graduate"; 
    } 
} 
+0

我想將列值傳遞給GetQual函數,但得到錯誤。如何將價值傳遞給功能? – user2240189

0

更改您的GetQual函數以獲取字符串而不是int。然後可以解析GetQual函數內部的字符串並進行相應處理。​​將始終返回一個字符串。

相關問題