puspita
puspita

Reputation: 109

How to get column header with non empty data in row?

I have a 15x15 dataGridView which on creation has empty (has no values) cells. Then, after user input the data, I want to get the column header which has non empty data in its row. I've been trying the following code:

DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
    bool empty = true;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
         if (row.Cells[col.Index].Value!=null)
         {
               empty = false;
               break;
         }
    }
    if (empty == false)
    {
         dt.Columns.Add(col.HeaderText);
    }
}

However, it doesn't appear to be working.

Upvotes: 0

Views: 1084

Answers (1)

puspita
puspita

Reputation: 109

DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
    bool empty = false;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells[col.Index].Value.ToString() == string.Empty)
        {
             empty = true;
        }
        break;
    }
    if (empty == false)
    {
        dt.Columns.Add(col.HeaderText);
    }
}

Upvotes: 1

Related Questions