开发者

Datatable and Datagridview

开发者 https://www.devze.com 2022-12-29 02:58 出处:网络
I have datatable table like id name address phoneno 1abckfjskl798798 2bcdkjdsfl808909 3899009 fjsh kjllkjl

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 ?


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).


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!


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消