C# 如何更改Gridview';是否根据ErrorMessage()方法设置文本颜色?
我有这些密码 在我的cs文件中:C# 如何更改Gridview';是否根据ErrorMessage()方法设置文本颜色?,c#,asp.net,gridview,colors,C#,Asp.net,Gridview,Colors,我有这些密码 在我的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.IsNullOrE
//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>
//在gridview中显示错误的方法
私有字符串错误消息(字符串输入)
{
{
//如果存在空值,将显示错误消息。
如果(!string.IsNullOrEmpty(输入))
返回输入;
}
返回“未输入值”;//我应该将其更改为红色
}
公共系统.Drawing.Color InStockColor(字符串inStock)
{
返回系统.Drawing.Color.Red;
}
//读取已发布csv文件的所有行并将这些行放入网格视图
var data=File.ReadAllLines(Server.MapPath(FilePath))
//按逗号分隔行的步骤
.Select(line=>line.Split(','))
.选择(列=>new{A=ErrorMessage(列[0])、B=ErrorMessage(列[1])、C=ErrorMessage(列[2])、D=ErrorMessage(列[3])、E=ErrorMessage(列[4])、F=ErrorMessage(列[5])、G=ErrorMessage(列[6])、H=ErrorMessage(列[7])、I=ErrorMessage(列[8]);
myGridView.DataSource=数据;
myGridView.DataBind();
到目前为止,我已经创建了一个方法,可以改变文本的颜色。现在,我如何实现这个方法?应该变为红色的文本是“未输入值!”这就是我解决问题的方法
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>";
}
当我使用你给我的方法时,它不起作用。它在我的部分给出了一个错误,即=ErrorMessage(列[0])、B=ErrorMessage(列[1])。。。部分。如果使用HTML标记,它将显示整个内容,如“+”中没有输入值“+”。因此,问题仍未解决=/您是否更正了呼叫的错误?您需要这样调用:ErrorMessage(列[0],行号,列号)您必须指定需要为红色的单元格(文本)的行和列。。
// 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();