如何获取行索引,以便我可以为单元格着色

时间:2021-05-30 04:15:31

I have a grid view which contains some sequence numbers. Each column needs to have unique number sequence. It is editable and if the user input number which is already in this particular column the cells are colored in red.

我有一个网格视图,其中包含一些序列号。每列需要具有唯一的数字序列。它是可编辑的,如果用户输入的数字已经在此特定列中,则单元格将显示为红色。

So far I color only the second cell of the 2 wrong cells (with same values). How can I color both cells. As far as I know I need to use the row index as well but I have no idea how..

到目前为止,我只对2个错误单元格的第二个单元格进行着色(具有相同的值)。如何为两个细胞着色。据我所知,我也需要使用行索引,但我不知道如何...

Here is the code

这是代码

            //colum2
            string sValue2 = e.Row.Cells[4].Text;

            if (Values2.Contains(sValue2))
            {
                // This value is a duplicate - color red
                e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
                 //Response.Write("Warning... Error in pad no: " +e.Row.Cells[2].Text+ " dupliated value: " +e.Row.Cells[3].Text);


            }
            else
            {
                Values2.Add(sValue2);
            }

and the same for all the 8 columns

对于所有8列都是一样的

Hopefully it's understandable. Thanks a lot in advance

希望这是可以理解的。非常感谢提前

2 个解决方案

#1


3  

If I get your question right, you would like to find out the RowIndex for the row which already contains the same data in the same cell, and change it's color to red.

如果我的问题是正确的,您希望找到已在同一单元格中包含相同数据的行的RowIndex,并将其颜色更改为红色。

You could write a method to change every cells color to red which contains that data. Something like:

您可以编写一种方法将每个单元格颜色更改为包含该数据的红色。就像是:

void colorCells(GridView GV, int CellIndex, string Text)
{
    foreach(GridViewRow row in GV.Rows)
    {
        if(row.Cells[CellIndex].Text == Text)
            row.Cells[CellIndex].BackColor = System.Drawing.Color.Red;
        //might use else to set to default color
    }
}

And then you could use like this:

然后你可以像这样使用:

    //colum2
    string sValue2 = e.Row.Cells[4].Text;
        if (Values2.Contains(sValue2))
        {
            // This value is a duplicate - color red
            colorCells(/* Get the reference for the grid view, e.g.: gridView1*/, 4, sValue2); 
        }
        else
        {
            Values2.Add(sValue2);
        }

#2


2  

Your question wasn't very clear but I'll try to help and respond from what I understood.

你的问题不是很清楚,但我会尽力帮助并回应我的理解。

There are different ways to get the row-index.

获取行索引有多种方法。

It depends on which event of the GridView control, your code-logic needs to be executed:

这取决于GridView控件的哪个事件,您的代码逻辑需要执行:

  • _RowDataBound: e.Row.RowIndex
  • _RowCommand: ((Control)e.CommandSource).NamingContainer as GridViewRow.RowIndex
  • _RowCommand :((Control)e.CommandSource).NamingContainer as GridViewRow.RowIndex

  • _RowEditing: e.NewEditIndex
  • _RowUpdating, _RowDeleting and _RowCancelingEdit: e.RowIndex
  • _RowUpdating,_RowDeleting和_RowCancelingEdit:e.RowIndex

Hope this helps.

希望这可以帮助。

Sample Code:

private bool IsCellToHighlight(GridViewCell cell)
{
bool highlightCell = false;
// Put your cell checking condition
// Example ..
highlightCell = (string.Compare(cell.Text, "Check Value", true) == 0);
return highlightCell;
}

private void HighlightCells(GridViewRow row)
{
for(int c=0; r<row.Cells.Count; c++)
{
if(this.IsCellToHighlight(row.Cells[c]))
{
// Apply required styling Code
// ..
}
}
}

Another way of doing this is using JQuery:

另一种方法是使用JQuery:

  1. At first just load the grid as is
  2. 首先按原样加载网格

  3. Then execute the same using JQuery on client-side
  4. 然后在客户端使用JQuery执行相同的操作

Check out these links:

看看这些链接:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=259

http://www.dotnetfunda.com/articles/article970-aspnet-gridview-jquery-tips-and-tricks-part-1.aspx

http://www.dotnetcurry.com/ShowArticle.aspx?ID=250


Disclaimer: This is just one way of doing. Though a bit crude, you could build over it.

免责声明:这只是一种做法。虽然有点粗糙,但你可以建立它。

#1


3  

If I get your question right, you would like to find out the RowIndex for the row which already contains the same data in the same cell, and change it's color to red.

如果我的问题是正确的,您希望找到已在同一单元格中包含相同数据的行的RowIndex,并将其颜色更改为红色。

You could write a method to change every cells color to red which contains that data. Something like:

您可以编写一种方法将每个单元格颜色更改为包含该数据的红色。就像是:

void colorCells(GridView GV, int CellIndex, string Text)
{
    foreach(GridViewRow row in GV.Rows)
    {
        if(row.Cells[CellIndex].Text == Text)
            row.Cells[CellIndex].BackColor = System.Drawing.Color.Red;
        //might use else to set to default color
    }
}

And then you could use like this:

然后你可以像这样使用:

    //colum2
    string sValue2 = e.Row.Cells[4].Text;
        if (Values2.Contains(sValue2))
        {
            // This value is a duplicate - color red
            colorCells(/* Get the reference for the grid view, e.g.: gridView1*/, 4, sValue2); 
        }
        else
        {
            Values2.Add(sValue2);
        }

#2


2  

Your question wasn't very clear but I'll try to help and respond from what I understood.

你的问题不是很清楚,但我会尽力帮助并回应我的理解。

There are different ways to get the row-index.

获取行索引有多种方法。

It depends on which event of the GridView control, your code-logic needs to be executed:

这取决于GridView控件的哪个事件,您的代码逻辑需要执行:

  • _RowDataBound: e.Row.RowIndex
  • _RowCommand: ((Control)e.CommandSource).NamingContainer as GridViewRow.RowIndex
  • _RowCommand :((Control)e.CommandSource).NamingContainer as GridViewRow.RowIndex

  • _RowEditing: e.NewEditIndex
  • _RowUpdating, _RowDeleting and _RowCancelingEdit: e.RowIndex
  • _RowUpdating,_RowDeleting和_RowCancelingEdit:e.RowIndex

Hope this helps.

希望这可以帮助。

Sample Code:

private bool IsCellToHighlight(GridViewCell cell)
{
bool highlightCell = false;
// Put your cell checking condition
// Example ..
highlightCell = (string.Compare(cell.Text, "Check Value", true) == 0);
return highlightCell;
}

private void HighlightCells(GridViewRow row)
{
for(int c=0; r<row.Cells.Count; c++)
{
if(this.IsCellToHighlight(row.Cells[c]))
{
// Apply required styling Code
// ..
}
}
}

Another way of doing this is using JQuery:

另一种方法是使用JQuery:

  1. At first just load the grid as is
  2. 首先按原样加载网格

  3. Then execute the same using JQuery on client-side
  4. 然后在客户端使用JQuery执行相同的操作

Check out these links:

看看这些链接:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=259

http://www.dotnetfunda.com/articles/article970-aspnet-gridview-jquery-tips-and-tricks-part-1.aspx

http://www.dotnetcurry.com/ShowArticle.aspx?ID=250


Disclaimer: This is just one way of doing. Though a bit crude, you could build over it.

免责声明:这只是一种做法。虽然有点粗糙,但你可以建立它。