Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

highlight duplicate rows in Datagridview

$
0
0

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;
    }

And to check for duplicate rows I am trying the following after binding the data to the dgv:

for (int X = 0; X <= dgvDuplicates.Rows.Count - 1; X++)
                    {
                        DataGridViewRow row = dgvDuplicates.Rows[X];
                        DataGridViewRow prevrow = dgvDuplicates.Rows[X - 1];

                        if (row.Cells["ENVA_APP_ID"].Value == prevrow.Cells["ENVA_APP_ID"].Value)   // Modify the if   Condtiong based on your requirement
                        {
                            // Heilight Row
                            row.DefaultCellStyle.BackColor = Color.Blue;
                            row.DefaultCellStyle.ForeColor = Color.Beige;
                        }
                    }
but this is crashing at this point - 
DataGridViewRow prevrow = dgvDuplicates.Rows[X - 1];
due to the fact that row 1 - 1 = 0 and no row exists at row 0


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>