2010-11-23 50 views
2

我有以下的GridView:如何用文本替換gridview中的複選框?

 <asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport"> 
     <Columns> 
      <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone"> 
       <ControlStyle Width="250px" /> 
      </asp:BoundField> 
      <asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" /> 
     </Columns> 
    </asp:GridView> 

第二行是數據庫中的一個布爾值,但我並不想顯示一個複選框或真\假給用戶。

我該如何顯示這樣的內容? 0 =不要撥打 1 =聯繫我們

回答

1

我最終只是使用OnRowDataBound來做到這一點。

<asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport" OnRowDataBound="OnRowDataBound"> 
    <Columns> 
     <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone"> 
      <ControlStyle Width="250px" /> 
     </asp:BoundField> 
     <asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" /> 
    </Columns> 
</asp:GridView> 


protected void OnRowDataBound(object sender, EventArgs e) 
{ 
    GridViewRowEventArgs ea = e as GridViewRowEventArgs; 
    if (ea.Row.RowType == DataControlRowType.DataRow) 
    { 
     DataRowView drv = ea.Row.DataItem as DataRowView; 
     Object ob = drv["Phone"]; 
     if (!Convert.IsDBNull(ob)) 
     { 
      bool iParsedValue = false; 
      if (bool.TryParse(ob.ToString(), out iParsedValue)) 
      { 
       TableCell cell = ea.Row.Cells[1]; 
       if (iParsedValue == false) 
       { 

        cell.Text = "Don't Call"; 
       } 
       else 
       { 
        cell.Text = "Call Us"; 
       } 

      } 
     } 
    } 
} 

現在它工作得很好。

3

您可以創建一個TemplateField而不是BoundField。

<asp:TemplateField HeaderText="Whatever"> 
    <ItemTemplate> 
     <asp:Literal ID="litTextValue" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 

然後,您可以把一些代碼內嵌顯示您想要的文本或處理RowDataBound事件做邏輯存在。

+0

我是沒有的與TemplateField運氣很好,所以我去了另一條路線(請參閱我的答案)。但是,我正在閱讀TemplateField,因爲我不經常使用它。 – InsertOldUserIDHere 2010-11-24 19:43:27

0

我做到這一點,它的工作

 <asp:Literal ID="isActive" runat="server" 
    Text='<%#Eval("isActive")==DBNull.Value ? 
    "inactive":Convert.ToBoolean(Eval("isActive"))?"active":"inactive" 
    %>'></asp:Literal> 

這是重要的組成部分。

文本= '<%#的eval( 「isActive」)== DBNull.Value 「無效」:Convert.ToBoolean(EVAL( 「isActive」)) 「活性」: 「無效」 %>'

希望有幫助。

0

你應該這樣做在SQL,而不是在這裏做它用一個CASE語句 這樣

CASE ToCall WHEN '1' THEN 'Call' ELSE 'Do not call' END AS ToCall

,然後用一個簡單的綁定字段如

<asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />