【问题标题】:How to change a Gridview's text colour according to the ErrorMessage() method?如何根据 Error Message() 方法更改 Gridview 文本颜色?
【发布时间】:2011-09-08 03:48:47
【问题描述】:

我有这些代码 在我的cs文件中:

   //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>

到目前为止,我已经创建了一个可以改变文本颜色的方法。现在,我该如何实现这个方法?应该变为红色的文本是“没有输入值!”

【问题讨论】:

    标签: c# asp.net gridview colors


    【解决方案1】:

    这就是我解决问题的方法!

    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>";
    } 
    

    但我不推荐使用这些标签,因为 &lt;font&gt; 已被弃用,您实际上将单元格的文本值设置为 html 代码。

    希望这会有所帮助!

    编辑:

    你能解释一下这段代码吗...mabe提供一些上下文 这是什么语法?

    您能解释一下 .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();
    

    【讨论】:

    • 我用你给我的方法还是不行。它在我的部分给出了一个错误,即 = ErrorMessage(columns[0]), B = ErrorMessage(columns[1])... 部分。如果使用 HTML 标签,它将显示整个内容,如 "" + "No value entered" + ""。所以还是没有解决=/
    • 您是否更正了通话内容?你需要这样调用: ErrorMessage(columns[0], row number, column number) 你必须指定你需要红色的单元格(文本)的行和列..
    猜你喜欢
    • 2017-03-09
    • 2018-06-19
    • 1970-01-01
    • 2015-01-04
    • 2016-06-02
    • 2020-09-16
    • 1970-01-01
    • 2022-10-18
    • 2014-07-16
    相关资源
    最近更新 更多