datagridview中的动态列计数 [英] Dynamic column count in datagridview

查看:127
本文介绍了datagridview中的动态列计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagridview中进行动态计数,我已按照以下方式完成此操作。但是我无法解决它,也许其他人会知道如何正确执行我的执行。



我希望它能够做到这一点;



列必须计算(如行)e.x.有30个列需要从0到30进行计数并检查每列1by1,现在数据表被放置在加载过程中,因此我们不能再从数据表中获取值,只需填充数据网格视图。



如果以某种方式我可以解决细胞[f](计数从0到30的细胞)而不是它将被解决但是显然我不能。



很难解释,但我希望你会理解它。



Iam making a dynamic count in datagridview, i've done this in the followed ways. yet i can't solve it, perhaps someone else will know how to do my execution right.

i want it to do the followed;

the columns have to be counted (as rows) e.x. there are 30 columns you need to count from 0 to 30 and check each column 1by1, now the datatable is disposed on the load procedure so we can't get values from the datatable anymore, just the datagridview which is populated.

if somehow i could solve the cells[f] (count the cells from 0 to 30) than it would be solved but apperently somehow i cannot.

its hard to explain but i'd hope you will understand it.

int columnz = datatable.columns.count;
columns.Text = columnz.ToString();







for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
    for (int f = 0; i <= Convert.ToInt64(columns.Text); i++)
    {
        if (dataGridView1.Rows[i].Cells[f].Value.ToString() == null)
        {
            dataGridView1.Rows.RemoveAt(i);
            i--;
        }
    }
}





我也试过做过细胞(f.tostring) ())...我不知道该怎么做。



i've also tried to do Cells(f.tostring())... i dont know what to do anymore.

推荐答案

尝试下面的代码

try below code
for (int row = 0; row < dataGridView1.Rows.Count; ++row) {
    bool isEmpty = false;
    for (int col = 0; col < dataGridView1.Columns.Count; ++col) {
        object value = dataGridView1.Rows[row].Cells[col].Value;
        if (value  == null || value  == DBNull.Value || String.IsNullOrWhitespace(value .ToString()) {
            isEmpty = true;
            break;
        }
    }
    if (isEmpty) {
        dataGridView1.Rows.RemoveAt(row--);
    }
}


这篇关于datagridview中的动态列计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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