mardi 21 avril 2015

Add specific rows from dataGridView depending on search value

Following issue: I have a datatable and a list which contains specific values.

routIds = col1Items.Distinct().ToList();
String searchValue = String.Empty;        
int rowIndex = -1;

for (int i = 0; i < routIds.Count; i++)
{
    searchValue = routIds[i];

    foreach (DataGridViewRow row in form1.dataGridView5.Rows)
    {
        if (row.Cells[form1.routingIdBox.Text].Value != null) // Need to check for null if new row is exposed
        {
            if (row.Cells[form1.routingIdBox.Text].Value.ToString().Equals(searchValue))
            {
                rowIndex = row.Index;

                foreach (DataGridViewColumn column in form1.dataGridView5.Columns)
                    dtRout.Columns.Add(column.Name);

                for (int k = 0; k < form1.dataGridView5.Rows.Count; k++)
                {
                    dtRout.Rows.Add();
                    for (int j = 0; j < form1.dataGridView5.Columns.Count; j++)
                    {
                        datTable.Rows[k][j] = form1.dataGridView5.Rows[rowIndex].Cells[j].Value;
                    }
                }                    
            }
        }
    }
}

I want to search the first column from my datagridview and check if it matches a specific value (from my array - routIds). If yes then I want to add the whole row into a datatable but I don't know how this works exactly. Tried around but I get exceptions (specific row not found).

Aucun commentaire:

Enregistrer un commentaire