一个GridView中的颜色单元格基于另一个GridView中的单元格值 [英] Color Cell in one GridView based on cell value in another GridView

查看:262
本文介绍了一个GridView中的颜色单元格基于另一个GridView中的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据GridView2的单元格的值为GridView1中的单元格着色。说:如果GridView1列[3]单元格的值=' '然后一个GridView2列[0] Row.Cells.BackColor = Color.Orange;。

下面的代码给整个GridView1上色,而不是特定的单元格。

pre $ protected void GridView2_OnRowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem,Enabled));
if(status ==False)
{
GridView1.RowStyle.BackColor = Color.Red;





解决方案

这是一个让你开始的片段。如果有一个值与GridView2中当前绑定的单元格值匹配,它将查看GridView1的每个单元格。当有匹配的时候,它会将单元格变成绿色。
$ b $ pre prerotected $ GridView2_RowDataBound(object sender,GridViewRowEventArgs e)
{
//检查行是否是数据行
if(e.RowType == DataControlRowType.DataRow)
{
//将行重新转换回datarowview
DataRowView row = e.Row.DataItem作为DataRowView;

//循环行中的所有列
for(int i = 0; i< row.DataView.Count; i ++)
{
string cellValue =行[I]的ToString();

//循环gridview1中的所有行
for(int j = 0; j< GridView1.Rows.Count; j ++)
{
//循环全部单元格gridview1
for(int k = 0; k< GridView1.Columns.Count; k ++)
{
string cellValueCompare = GridView1.Rows [j] .Cells [k] .Text ;

//比较值和颜色单元格
if(cellValue == cellValueCompare)
{
GridView1.Rows [j] .Cells [k] .BackColor = Color 。绿色;





$ b code $ pre


如果GridView1中的列是
BoundField TemplateField ,自动生成的列不能被
搜索到。



I am trying to color a cell in GridView1 based on a value of a cell of a GridView2. Say, " If the GridView1 Column[3] Cell value = ' ' then a GridView2 Column[0] Row.Cells.BackColor = Color.Orange; ".

The following code colors the entire GridView1 and not a particular cell.

protected void GridView2_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Enabled"));
            if (status == "False")
            {
                GridView1.RowStyle.BackColor = Color.Red;

            }

        }
   }

解决方案

Here is a snippet to get you started. It will look in every cell of GridView1 if there is a value that matches with the cell value currently being bound in GridView2. When there is a match it will color the cell green.

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the row back to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //loop all columns in the row
        for (int i = 0; i < row.DataView.Count; i++)
        {
            string cellValue = row[i].ToString();

            //loop all rows in gridview1 
            for (int j = 0; j < GridView1.Rows.Count; j++)
            {
                //loop all cells in gridview1
                for (int k = 0; k < GridView1.Columns.Count; k++)
                {
                    string cellValueCompare = GridView1.Rows[j].Cells[k].Text;

                    //compare values and color cell
                    if (cellValue == cellValueCompare)
                    {
                        GridView1.Rows[j].Cells[k].BackColor = Color.Green;
                    }
                }
            }
        }
    }
}

This will only work if the columns in GridView1 are either BoundField or TemplateField, auto-generated columns cannot be searched.

这篇关于一个GridView中的颜色单元格基于另一个GridView中的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆