Datatable and Datagridview

I have datatable table

like

id name address phoneno 
1  abc  kfjskl  798798
2  bcd  kjdsfl  808909
3               899009
   fjsh kjllkjl
5  jfkd 

      

And I am showing this value in datagridview by code

dataGridView1.ColumnCount = Table.Columns.Count;
dataGridView1.RowCount = Table.Rows.Count;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
    for (int j = 0; j < dataGridView1.ColumnCount; j++)
    {
        dataGridView1[j, i].Value = Table.Rows[i][j].ToString();
    }
}

      

Now I don't want to display a row with some missing value, for example if I do this then the datagridview looks like

 1  abc  kfjskl  798798
 2  bcd  kjdsfl  808909

      

How can i do this?

+2


a source to share


3 answers


This will do what you need (I think):

dataGridView1.ColumnCount = Table.Columns.Count;
for (int i = 0; i < Table.Rows.Count; i++)
{
    bool valueMissing = false;
    DataGridViewRow row = new DataGridViewRow();
    for (int j = 0; j < dataGridView1.ColumnCount; j++)
    {
        if (Table.Rows[i][j].ToString() == "")
        {
            valueMissing = true;
            break;
        }
        else
        {
            row.Cells[j].Value = Table.Rows[i][j];
        }
    }
    if (!valueMissing)
    {
        dataGridView1.Rows.Add(row);
    }
}

      



This may need to be changed to check for null values ​​in Table.Rows[i][i]

(in addition to just checking for empty string values).

+1


a source


I think your best bet is to use DataView between DataGridView and DataTable. The DataView allows you to filter values ​​using and expression without affecting the original DataTable. To filter out all empty columns, you can create a view using this expression:

DataView view = new DataView(table);

view.RowFilter = "ISNULL(id, '') <> '' AND ISNULL(name, '') <> '' AND ISNULL(address, '') <> '' AND ISNULL(phoneno, '') <> ''";

      

The ISNULL function replaces the column value with the specified string if the column value is null. Thus, the filter replaces each NULL value with empty strings, therefore excluding NULL or Empty columns.

Then you can use this view to evaluate the values ​​in the grid instead of using the datatable type:



dataGridView1.ColumnCount = view.Columns.Count;
dataGridView1.RowCount = view.Rows.Count;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
  for (int j = 0; j < dataGridView1.ColumnCount; j++)
  {
    dataGridView1[j, i].Value = view.Rows[i][j].ToString();
  }
}

      

Instead of using loops to provide values ​​in the grid, you can simply use data binding. It's much easier:

dataGridView1.DataSource = view;

      

Hope it helps!

+1


a source


I'm not sure what I understand, but you can use DataTable.Select

to filter any rows with missing values ​​before inserting them into the datagrid.

Or else, instead of using a for loop on strings, just use a foreach loop on the rows in the table and add the rows one by one with dataGridView.Rows.Add(newRow)

.

0


a source







All Articles