gridview根据值更改文本颜色

时间:2021-11-09 20:15:21

I'm using onRowDataBound to change the text color of text as below but facing some issue..

我正在使用onRowDataBound来更改文本的文本颜色,如下所示,但面临一些问题..

protected void OnRowDataBound123(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TableCell statusCell1 = e.Row.Cells[1];
        if (statusCell1.Text != "-")
        {
            string[] a = statusCell1.Text.Split('/');
            if (a[0] != a[1])
            {
                statusCell1.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}

gridview根据值更改文本颜色

I'm getting error in Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array. Thing i want to do here is compare the value before and after slash "/" if doesn't match to each other then the text color will become red,else black color.

我在异常详细信息中遇到错误:System.IndexOutOfRangeException:索引超出了数组的范围。我想在这里做的是比较斜杠前后的值“/”如果彼此不匹配则文本颜色将变为红色,否则为黑色。

3 个解决方案

#1


0  

 protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        //System.Diagnostics.Debugger.Launch();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            Label lblparent = (Label)GridView1.Rows[i].FindControl("lblRate");
            if (lblparent.Text != "-")
            {
                string[] a = lblparent.Text.Split('/');
                if (a[0] != a[1])
                {
                    lblparent.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lblparent.ForeColor = System.Drawing.Color.Green;
                }
            }


        }
    }

this code solved my problem.

这段代码解决了我的问题。

#2


0  

It seems you're trying to apply a String.Split on a TableCell

看来你正在尝试在TableCell上应用String.Split

Try statusCell1.Text.Split('/')

(or something similar to .Text if that doesn't work.)

(或类似于.Text的东西,如果这不起作用。)

//Creating values for testing
//These are just for testing do not place this in your code
TableCell statusCell1 = new TableCell();
statusCell1.Text = "blah/blah blah";

//##################################
//These are the only lines you need
string[] splitString = statusCell1.Text.Split('/');
string resultingString = splitString[0];
string resultingString2 = splitString[1];
//##################################

//These are just for testing do not place this in your code
Console.WriteLine(resultingString);
Console.WriteLine(resultingString2);

//prints out: 
//blah 
//blah blah

#3


0  

Directly get value from row cell, try below, it is tested:

直接从行单元格获取值,请尝试以下,测试:

protected void OnRowDataBound123(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string cellValue = statusCell1 = e.Row.Cells[1].Text;
        if (cellValue != "-")
        {
            string[] a = cellValue.Split('/');
            if (a[0] != a[1])
            {
                e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}

#1


0  

 protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        //System.Diagnostics.Debugger.Launch();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            Label lblparent = (Label)GridView1.Rows[i].FindControl("lblRate");
            if (lblparent.Text != "-")
            {
                string[] a = lblparent.Text.Split('/');
                if (a[0] != a[1])
                {
                    lblparent.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lblparent.ForeColor = System.Drawing.Color.Green;
                }
            }


        }
    }

this code solved my problem.

这段代码解决了我的问题。

#2


0  

It seems you're trying to apply a String.Split on a TableCell

看来你正在尝试在TableCell上应用String.Split

Try statusCell1.Text.Split('/')

(or something similar to .Text if that doesn't work.)

(或类似于.Text的东西,如果这不起作用。)

//Creating values for testing
//These are just for testing do not place this in your code
TableCell statusCell1 = new TableCell();
statusCell1.Text = "blah/blah blah";

//##################################
//These are the only lines you need
string[] splitString = statusCell1.Text.Split('/');
string resultingString = splitString[0];
string resultingString2 = splitString[1];
//##################################

//These are just for testing do not place this in your code
Console.WriteLine(resultingString);
Console.WriteLine(resultingString2);

//prints out: 
//blah 
//blah blah

#3


0  

Directly get value from row cell, try below, it is tested:

直接从行单元格获取值,请尝试以下,测试:

protected void OnRowDataBound123(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string cellValue = statusCell1 = e.Row.Cells[1].Text;
        if (cellValue != "-")
        {
            string[] a = cellValue.Split('/');
            if (a[0] != a[1])
            {
                e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}