Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改gridview单元格颜色的可重用方法_C#_Asp.net - Fatal编程技术网

C# 更改gridview单元格颜色的可重用方法

C# 更改gridview单元格颜色的可重用方法,c#,asp.net,C#,Asp.net,我需要根据数据值更改gridview单元格的颜色。使用Gridviews RowDataBound事件中的datarow视图和if语句(见下文),我可以很容易地做到这一点,但是我需要在30列上执行此操作,这将是相当冗长的,如果业务规则发生更改,那么更改将非常困难。如何将以下内容封装到一个可重用的方法中,我可以调用该方法并只传入数据列和单元格索引 protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)

我需要根据数据值更改gridview单元格的颜色。使用Gridviews RowDataBound事件中的datarow视图和if语句(见下文),我可以很容易地做到这一点,但是我需要在30列上执行此操作,这将是相当冗长的,如果业务规则发生更改,那么更改将非常困难。如何将以下内容封装到一个可重用的方法中,我可以调用该方法并只传入数据列和单元格索引

    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem != null)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;

            int A = Int32.Parse(drv["A"].ToString());
            if (A <= 74)
            {
                e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
            } 
        }
    }
受保护的void gv1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.DataItem!=null)
{
DataRowView drv=(DataRowView)e.Row.DataItem;
inta=Int32.Parse(drv[“A”].ToString());

如果(A找到了答案-Meckley先生让我走上了正确的道路,我的工作(如果不雅观)解决方案是:

    public void SetColor2(GridViewRow row, string columnName, int cellIndex)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            int number = Convert.ToInt32(columnName);
            if (number == 0)
            {
                row.Cells[cellIndex].Text = "";
                return;
            }
            else if ((number > 0) && (number <= 74))
            {
                row.Cells[cellIndex].BackColor = System.Drawing.Color.Red;
                row.Cells[cellIndex].ForeColor = System.Drawing.Color.Black;
                return;
            }
        }
    }

我将考虑创建一个颜色枚举,然后根据条件进行设置。将system.Drawing.Color.Red替换为enum.Color drv也必须更改,或者必须创建一个数组或列表或枚举,该数组或列表或枚举还可以确定要更改哪些字段..即使它是.config驱动的..总线有哪些例如,我会将颜色作为参数添加到方法中,至少因为它的名称;)@TimSchmelter要将这段代码重构为可重用的代码,还有很多工作要做。这完全取决于预期的重用程度。考虑到文章的上下文和当前的代码,我认为这是最适用的解决方案。
    public void SetColor2(GridViewRow row, string columnName, int cellIndex)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            int number = Convert.ToInt32(columnName);
            if (number == 0)
            {
                row.Cells[cellIndex].Text = "";
                return;
            }
            else if ((number > 0) && (number <= 74))
            {
                row.Cells[cellIndex].BackColor = System.Drawing.Color.Red;
                row.Cells[cellIndex].ForeColor = System.Drawing.Color.Black;
                return;
            }
        }
    }
    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem != null)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            SetColor2(e.Row, drv["A"].ToString(), 2);
            SetColor2(e.Row, drv["B"].ToString(), 3);
            etc...
        }
    }