Datagridview问题在C#.NET中删除空格 [英] Datagridview problem removing blank space in C#.NET

查看:255
本文介绍了Datagridview问题在C#.NET中删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上。我在这段代码上遇到了问题。我试图检查数据是否为空,以便将其绑定到datagridview。这是到目前为止的代码



Hai. I'm having a problem on this code. i tried to check if the data is empty or not in order to bind it to datagridview. this is the code so far

dbSumMetName.RowCount = form3.dbMetName.RowCount;
         foreach (DataGridViewRow row in form3.dbMetName.Rows)
         {
             foreach (DataGridViewColumn col in form3.dbMetName.Columns)
             {
                 {
                     dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
                 }
             }
         }





对于这段代码,我试着让它像这样< br $>




For this code, i try to make it like this

dbSumMetName.RowCount = form3.dbMetName.RowCount;
         foreach (DataGridViewRow row in form3.dbMetName.Rows)
         {
             foreach (DataGridViewColumn col in form3.dbMetName.Columns)
             {
                 {
                     if (row.Cells[col.Name].Value != null)
                     dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
                 }
             }
         }



它仍显示空白区域。


it still showing the empty space.

推荐答案

嗯...嗯。

让我们从逻辑上看一下:

Well... um.
Let's look at this logically:
foreach (DataGridViewRow row in form3.dbMetName.Rows)
{
...
}

查看每一行

Looks at every row

foreach (DataGridViewColumn col in form3.dbMetName.Columns)
{
...
}

查看每一行中的每一列。 />
然后:

Looks at every column in every row.
And then:

if (row.Cells[col.Name].Value != null)
dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;



那么......如果您不希望放入单元格中的值为空,那么您在单元格中放置的值是多少?



什么都没有。



否则称为 null ...所以如果new值为null,则保留旧的null值...

也许,你想要这样的东西:


So...what value are you putting in a cell if the value you don't want to put in the cell is null?

Nothing at all.

Otherwise known as null...so if the "new" value is null, you leave the old, null value...
Perhaps, you want something like this:

foreach (DataGridViewRow row in form3.dbMetName.Rows)
{
    foreach (DataGridViewColumn col in form3.dbMetName.Columns)
    {
        if (row.Cells[col.Name].Value != null)
        {
            dbSumMetName.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
        }
        else
        {
            dbSumMetName.Rows[row.Index].Cells[col.Name].Value = "????";
        }
    }
}







如果假设有两列没有值,如何删除1整行。有些像这样的代码




"how to remove 1 whole row if let say there are two column and two column has no value. some sort like this code"

DataGridView1.Rows.Remove(_row);









嗯......首先是:你不能使用foreach。它需要修改集合,并且不允许在foreach中使用。

因此,如果你想要删除整行,如果它的所有列都是空的:





Well...first thing is: you can't use foreach. It would require modifying the collection, and that is not allowed within a foreach.
So, if you want to get rid of the whole row if all it's columns are empty:

for (int i = form3.dbMetName.Rows.Count - 1; i >= 0; i--)
    {
    bool isBlank = true;
    DataGridViewRow row = form3.dbMetName.Rows[i];
    foreach (DataGridViewCell cell in row.Cells)
        {
        if (cell.Value != null && cell.Value.ToString() != "")
            {
            isBlank = false;
            break;
            }
        }
    if (isBlank)
        {
        form3.dbMetName.Rows.Remove(row);
        }
    }



从最后一行走到第一行比上一行更安全,因为你删除行并随机播放你检查过的那些!


Going down from the last row to the first is safer than going up, as you remove rows and shuffle ones you have checked!


好的,我可能错了,我的意思是它不是你想要的精确解决方案,但我仍然在努力。

请参阅此代码将消除包含空值的所有列。

Okay, I might be wrong, I mean it can't be exact solution as you want but still I'm trying.
See this code will eliminate all the column which contains null values.
public DataTable CheckDataTableColumn(DataTable dt)
{
    bool flag = false;
    int counter = 0;

EXIT:
    for (int i = counter; i < dt.Columns.Count; i++)
    {
        for (int x = 0; x < dt.Rows.Count; x++)
        {
            if (string.IsNullOrEmpty(dt.Rows[x][i].ToString()))
                flag = true;    // empty value
            else
            {
                flag = false;
                counter = i + 1;

                goto EXIT;
            }
        }
        if (flag)
        {
            dt.Columns.Remove(dt.Columns[i]);
            i--;
        }
    }

    return dt;
}



希望对您有所帮助。



-KR


Hope might be helpful to you.

-KR


这篇关于Datagridview问题在C#.NET中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆