Remove columns with Null from Datagridview

Asked

Viewed 38 times

0

I already used "IS NOT NULL" in Query’s Select, but it removes the entire row and in this case, I would like to remove the column only. This table has more than 70 columns and Null Fields may vary, but when a column has null, it will null the entire column. I also tried via code, but to no avail.inserir a descrição da imagem aqui

1 answer

0

It took me two iterations to do this work, one to go through the columns of the datagridview and store the name of the columns that have the row with null value and the second only to remove. This is because if I removed the column in the first iteration, the collection would be modified and the result would not be as desired.

        //lista para ir armazenando o nome das colunas que irão ser removidas
        List<string> columnNames = new List<string>();

        //percorre as colunas do datagridview
        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {
            //verifica se o valor da primeira linha é igual a DBNull.Value
            if (dataGridView1[column.Index, 0].Value == DBNull.Value)
            {
                //adiciona a coluna a lista para depois ser removida
                columnNames.Add(column.Name);
            }
        }

        //percorre a lista para ir removendo as colunas
        foreach (string columnName in columnNames)
        {
            dataGridView1.Columns.Remove(dataGridView1.Columns[columnName]);
        }
  • Very good! Thanks Marcos! Solved a problem I was days away! Congratulations!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.