Ich bin Neuling in ASP.NET und C#. Ich möchte Hintergrundfarbe zu allen Zellenwerten von Gridview hinzufügen, die mit dem oberen Buchstaben beginnen. Hier ist meine Quellcode:Hintergrundfarbe zu allen Zellenwerten von Gridview hinzufügen, die mit Großbuchstaben beginnt
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell = e.Row.Cells[2];
string entity = cell.Text.ToString();
if (entity[0] >= 'A' && entity[0] <= 'Z')
{
cell.BackColor = Color.Yellow;
}
}
}
Aber ich habe diesen Fehler:
Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.
Source Error:
Line 85: TableCell cell = e.Row.Cells[1];
Line 86: string entity = cell.Text.ToString();
Line 87: if (entity[0] >= 'A' && entity[0] <= 'Z')
Line 88: {
Line 89: cell.BackColor = Color.Yellow;
Source File: c:\Users\Eric\Default.aspx.cs Line: 87
Bitte helfen Sie mir, diesen Fehler zu lösen
Danke an alle
'char' hat eine' IsUpper' Methode. Sie könnten 'char.IsUpper (entity [0])' verwenden, stellen Sie einfach sicher, dass 'entity' nicht null ist und mindestens 1 Zeichen enthält. –