突出显示Datagridview中的重复行 [英] highlight duplicate rows in Datagridview

查看:106
本文介绍了突出显示Datagridview中的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用按环境和应用程序名称排序的SQL查询来填充DataTable,对于返回的数据,我想要突出显示应用程序名称相同的任何重复行。以下是我填充datagridview的方法:

 尝试 
{
< span class =code-comment> // 调用DataProcessor类以从数据库中获取记录
DataProcessor dp = new DataProcessor();
DataTable dt = dp.getRecs(txtEnvID.Text);

// 如果返回记录
if (dt.Rows.Count > 0
{
// 在绑定到 $之前从数据表中删除下面的cols b $ b // datagridview对象 - 不希望显示这些详细信息
dt.Columns .Remove( ENV_ROWVERSION_TS);
dt.Columns.Remove( ENVA_ROWVERSION_TS);
dt.Columns.Remove( APP_ROWVERSION_TS);
dt.Columns.Remove( ENVA_ENVIRONMENT_ID1);
dt.Columns.Remove( ENVA_APP_ID1);
dt.Columns.Remove( CountOf);

// 将数据绑定到显示网格
dgvDuplicates.DataSource = dt;
}





任何人都可以告诉我如何检查重复行吗?



谢谢!

解决方案

用这段代码解决了这个问题 - 希望以后能帮到别人:

< pre lang =c#> public void HighlightDuplicate(DataGridView grv)
{
// 使用currentRow与进行比较
for int currentRow = 0 ; currentRow < grv.Rows.Count - 1 ; currentRow ++)
{
DataGridViewRow rowToCompare = grv.Rows [currentRow];
// 将otherRow指定为currentRow + 1
for int otherRow = currentRow + 1 ; otherRow < grv.Rows.Count; otherRow ++)
{
DataGridViewRow row = grv.Rows [otherRow];

bool duplicateRow = true ;
// 比较两行之间的单元格ENVA_APP_ID
if (!rowToCompare.Cells [ ENVA_APP_ID]。 .Equals(row.Cells [ ENVA_APP_ID]。值))
{
duplicateRow = false ;
break ;
}
// 如果ENVA_APP_ID匹配,则突出显示currentRow和otherRow
if (duplicateRow)
{
rowToCompare.DefaultCellStyle.BackColor = Color.Red;
rowToCompare.DefaultCellStyle.ForeColor = Color.Black;
row.DefaultCellStyle.BackColor = Color.Red;
row.DefaultCellStyle.ForeColor = Color.Black;
}
}
}
}


CellFormatting 会做的伎俩在这种情况下。检查每一行的应用程序名称列并更改颜色,如...

  private  < span class =code-keyword> void  MyDataGridView_CellFormatting( object  sender,DataGridViewCellFormattingEventArgs e)
{

< span class =code-keyword> if (e.RowIndex == rowIndexToHighlight)
{
e.CellStyle.BackColor = Color.Green;
}

}



这些也可以帮到你..

http://forums.asp.net/p/1654794/4657253.aspx/1 ?重新+高亮+重复+值+ +网格视图 [ ^ ]

http://www.nullskull.com/q/10063889/gridview-formatting-for-desktop-app.aspx [ ^ ]


Hi I'm populating a DataTable using a SQL query which is ordered by Environment AND Application Name, for the returned data i'd like to highlight any duplicate rows where the Application Name is the same. Here's how I'm populating my datagridview:

try
{
    //Call DataProcessor class to get the records from the database
    DataProcessor dp = new DataProcessor();
    DataTable dt = dp.getRecs(txtEnvID.Text);

    //if records are returned
    if (dt.Rows.Count > 0)
    {
        //remove the below cols from the datatable before binding to the
        //datagridview object - do not wish to display these details
        dt.Columns.Remove("ENV_ROWVERSION_TS");
        dt.Columns.Remove("ENVA_ROWVERSION_TS");
        dt.Columns.Remove("APP_ROWVERSION_TS");
        dt.Columns.Remove("ENVA_ENVIRONMENT_ID1");
        dt.Columns.Remove("ENVA_APP_ID1");
        dt.Columns.Remove("CountOf");

        //bind datatable to display grid
        dgvDuplicates.DataSource = dt;
    }



Can anyone enlighten me as to how I check for duplicate rows?

thank you!

解决方案

Solved this with this code - hope this helps someone else in future:

public void HighlightDuplicate(DataGridView grv)
{
    //use the currentRow to compare against
    for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
    {
        DataGridViewRow rowToCompare = grv.Rows[currentRow];
        //specify otherRow as currentRow + 1
        for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count; otherRow++)
        {
            DataGridViewRow row = grv.Rows[otherRow];

            bool duplicateRow = true;
            //compare cell ENVA_APP_ID between the two rows
            if (!rowToCompare.Cells["ENVA_APP_ID"].Value.Equals(row.Cells["ENVA_APP_ID"].Value))
            {
                duplicateRow = false;
                break;
            }
            //highlight both the currentRow and otherRow if ENVA_APP_ID matches 
            if (duplicateRow)
            {
                rowToCompare.DefaultCellStyle.BackColor = Color.Red;
                rowToCompare.DefaultCellStyle.ForeColor = Color.Black; 
                row.DefaultCellStyle.BackColor = Color.Red;
                row.DefaultCellStyle.ForeColor = Color.Black; 
            }
        }
    }
}


CellFormatting would do the trick in this case.So check application name column for each row and change color like..

private void MyDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

        if (e.RowIndex == rowIndexToHighlight)
        {
            e.CellStyle.BackColor = Color.Green;
        }

    }


These also help you..
http://forums.asp.net/p/1654794/4657253.aspx/1?Re+Highlight+duplicate+values+in+gridview[^]
http://www.nullskull.com/q/10063889/gridview-formatting-for-desktop-app.aspx[^]


这篇关于突出显示Datagridview中的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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