Datagridview到数组缺少一行。 [英] Datagridview to array missing one row.

查看:74
本文介绍了Datagridview到数组缺少一行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 DataGridView 带有 CheckBox 第一列。



我使用以下Linq获取所有选中的行。



 DataGridViewRow [] drs = dgvMain.Rows.Cast< DataGridViewRow>()。其中​​(x = < span class =code-keyword>> (!Convert.IsDBNull(x.Cells [ 0 ]。Value)&& Convert。 ToBoolean(x.Cells [ 0 ]。Value)))。ToArray(); 





但不知何故结果总是错过最后一行!!!



但是 ,如果我选择另一个卷(不检查),在我运行该行之前,最后一行出现了!!!



请有人请太善良了,告诉我哪里做错了!?



非常感谢!!!



我尝试了什么:



我检查了各种条件无济于事!

解决方案
我们无法分辨:这很可能取决于您的数据,而且我们无权访问。



因此,它将会上升对你来说。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。



在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。检查DataSource内容,并找出它将要处理的内容。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行它并仔细观察)以找出原因。看看阵列,看看它包含的确切含义。



对不起,但我们不能为你做到这一点 - 时间让你学习一个新的(非常,非常有用)技能:调试!


George Swan是对的! DataGridView IS仍处于编辑模式!



因此在Linq之前添加

 dgvMain.EndEdit(); 

解决了这个问题!


查看此代码:



 DataTable dt =  new  DataTable(); 
dt.Columns.Add( typeof Boolean ));
dt.Columns.Add( B typeof string ));
dt.Rows.Add( true A);
dt.Rows.Add( true B);
dt.Rows.Add( false C);
dt.Rows.Add( true d);
dt.Rows.Add( false E);
dgvMain.DataSource = dt;

DataGridViewRow [] drs = dgvMain.Rows.Cast< DataGridViewRow>()。其中​​(x = >
(! Convert.IsDBNull(x.Cells [ 0 ]。值)&& Convert.ToBoolean(x.Cells [ 0 ]。价值)))
.ToArray();


I have a DataGridView with a CheckBox first column.

I use the following Linq to get all the checked rows.

DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>(!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value))).ToArray();



But somehow the result ALWAYS missing the last row!!!

BUT, if I select another roll (not checking it), before I run the line, the last row showed up!!!

Could somebody please be so kind and tell me where did I do wrong!?

Much appreciated!!!

What I have tried:

I had checked all kind of where condition to no avail!

解决方案

We can't tell: this is very likely dependant on your data, and we don't have any access to that.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Examine the DataSource content, and find out exactly what it will be processing. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Look at the array and see exactly what it contains.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


George Swan is right! The DataGridView IS still in edit mode!

So adding

dgvMain.EndEdit();

before the Linq solved the problem!


Review This Code:

DataTable dt = new DataTable();
            dt.Columns.Add("A", typeof(Boolean));
            dt.Columns.Add("B", typeof(string));
            dt.Rows.Add(true,"A");
            dt.Rows.Add(true, "B");
            dt.Rows.Add(false, "C");
            dt.Rows.Add(true, "D");
            dt.Rows.Add(false, "E");
            dgvMain.DataSource = dt;

 DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>
           (!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value)))
           .ToArray();


这篇关于Datagridview到数组缺少一行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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