저는 ASP.NET과 C#에서 초보자입니다. 위 문자로 시작하는 gridview의 모든 셀 값에 배경색을 추가하고 싶습니다. 여기 내 소스 코드 :gridview의 모든 셀 값에 배경색을 추가 하시길 바랍니다.
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;
}
}
}
하지만이 오류가있어 :
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
날이 오류를 해결하기 위해 도와주세요
이 문제는 이것으로 모든
'char'는'IsUpper' 메소드를 가지고 있습니다. 당신은'char.IsUpper (entity [0])'를 사용할 수 있습니다. 단지'entity'가 null이 아니고 적어도 하나의 문자를 가지고 있는지 확인하십시오. –