2011-09-08 70 views
0

我有這些代碼 在我的CS文件:如何根據ErrorMessage()方法更改Gridview的文本顏色?

//A method to display errors in the gridview 
    private string ErrorMessage(string input) 

     { 
      { 
       //if there are null values, error message will be displayed. 
       if (!string.IsNullOrEmpty(input)) 
        return input; 

      } 
      return "No value entered";// I am supposed to change this to red colour 


     } 

    public System.Drawing.Color InStockColor(string inStock) 
    { 
     return System.Drawing.Color.Red; 
    } 




// to read all lines of the posted csv file and put the lines in the grid view 
     var data = File.ReadAllLines(Server.MapPath(FilePath)) 
      // to split the lines according to commas 
      .Select(line => line.Split(',')) 

      .Select(columns => new { A = ErrorMessage(columns[0]), B = ErrorMessage(columns[1]), C = ErrorMessage(columns[2]), D = ErrorMessage(columns[3]), E = ErrorMessage(columns[4]), F = ErrorMessage(columns[5]), G = ErrorMessage(columns[6]), H = ErrorMessage(columns[7]), I = ErrorMessage(columns[8]) }); 


     myGridView.DataSource = data; 
     myGridView.DataBind(); 



<asp:GridView ID="myGridView" runat="server" CellPadding="4" 
    EnableModelValidation="True" ForeColor="#333333" GridLines="None" 
    Width="414px" align="center"> 
    <AlternatingRowStyle BackColor="White" /> 

    <EditRowStyle BackColor="#2461BF" /> 
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#EFF3FB" /> 
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 

</asp:GridView> 

到目前爲止,我已經建立,這將改變文字顏色的方法。現在,我該如何實現這個方法?應該更改爲紅色的文本是「沒有輸入值!」

回答

0

這就是我將如何解決問題!

using System.Drawing; 

private string ErrorMessage(string input, int rowNr, int colNr)   
{    
    if (!string.IsNullOrEmpty(input)) 
     return input; 

    myGridView.Rows[rowNr].Cells[colNr].ForeColor = Color.Red; 
    return "No value entered"; 
} 

另一種方法是添加HTML標籤這樣的...

using System.Drawing; 

private string ErrorMessage(string input, int rowNr, int colNr)   
{    
    if (!string.IsNullOrEmpty(input)) 
     return input; 
    return "<font color='Red'>" + "No value entered" + "</font>"; 
} 

,但我不建議標籤,因爲<font>已被棄用,你居然設置單元格中的文本值html代碼。

希望這會有所幫助!

編輯:

能否請您解釋一下這段代碼...馬貝提供一些背景 什麼樣的語法是什麼?

你能解釋一下.Select(...)命令是如何工作的嗎?他們做什麼?

// to read all lines of the posted csv file and put the lines in the grid view 
     var data = File.ReadAllLines(Server.MapPath(FilePath)) 
      // to split the lines according to commas 
      .Select(line => line.Split(',')) 

      .Select(columns => new { A = ErrorMessage(columns[0]), B = ErrorMessage(columns[1]), C = ErrorMessage(columns[2]), D = ErrorMessage(columns[3]), E = ErrorMessage(columns[4]), F = ErrorMessage(columns[5]), G = ErrorMessage(columns[6]), H = ErrorMessage(columns[7]), I = ErrorMessage(columns[8]) }); 


     myGridView.DataSource = data; 
     myGridView.DataBind(); 
+0

當我使用您給我的方法時,它沒有工作。它在我的部分給出錯誤= ErrorMessage(列[0]),B = ErrorMessage(列[1])...部分。如果使用HTML標記,它將顯示整個內容,如「」+「沒有輸入值」+「」。因此,它仍然沒有解決=/ – Mark20

+0

你更正了電話的?你需要像這樣調用:ErrorMessage(列[0],行號,列號),你必須指定行和單元格的列(文本),你需要紅色.. –