2011-09-08 141 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],行号,列号),你必须指定行和单元格的列(文本),你需要红色.. –

相关问题