views:

75

answers:

3

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 displaying this value in the 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 row that has some missing value like If I will do that then the datagridview will look like

 1  abc  kfjskl  798798
 2  bcd  kjdsfl  808909

How can I do that ?

A: 

I'm not sure if I understand, but you could possibly use DataTable.Select to filter out any rows with missing values before you insert them in the datagrid.

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

ho1
@ho,How do I filter the rows without missing values ?
Harikrishna
@ho,What is the benefit using foreach loop here ?
Harikrishna
If you use a foreach (or for but I think a foreach is neater) on the rows in the Table rather than the rows in the DataGRidView, you don't have to add the rows to the grid in advance and so can filter out the rows you don't want.
ho1
Regarding your first question, I might have misunderstood, but I thought you wanted to only show rows that had values in all fields?
ho1
+1  A: 

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

DataView view = new DataView(table);

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

The ISNULL function replaces a column value whit the specified string, if the column value is null. So the filter is replacing every NULL value with empty strings, so excluding NULL or Empty columns.

Then you can use this view to assing values to the grid, instead of using the datatable:

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

Also instead of using loops to provide values to the grid, you can simply use databinding. It's much simpler:

dataGridView1.DataSource = view;

Hope it helps!

iCe
You can see valid expression documentation here: http://msdn.microsoft.com/es-es/library/system.data.datacolumn.expression(v=VS.80).aspx
iCe
@iCe,but everytime fields name are different.
Harikrishna
+1 for using DataView and Databinding.
Ken
@Harikrishna Why you cannot just rename column names to your liking? If the DataTable is created manually you always can. And if it's the result of a SQL you can use aliases.I mean, you can name your columns like 'Column1', 'Column2', and so on.Maybe update the question with the concrete DataTable loading example?
iCe
A: 

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 modified to check for null values in Table.Rows[i][i] (in addition to just checking for empty string values).

MusiGenesis