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>
到目前爲止,我已經建立,這將改變文字顏色的方法。現在,我該如何實現這個方法?應該更改爲紅色的文本是「沒有輸入值!」
當我使用您給我的方法時,它沒有工作。它在我的部分給出錯誤= ErrorMessage(列[0]),B = ErrorMessage(列[1])...部分。如果使用HTML標記,它將顯示整個內容,如「」+「沒有輸入值」+「」。因此,它仍然沒有解決=/ – Mark20
你更正了電話的?你需要像這樣調用:ErrorMessage(列[0],行號,列號),你必須指定行和單元格的列(文本),你需要紅色.. –