SelectedRows.Count VS Rows.GetRowCount(DataGridViewElementStates.Selected) [英] SelectedRows.Count vs Rows.GetRowCount(DataGridViewElementStates.Selected)

查看:571
本文介绍了SelectedRows.Count VS Rows.GetRowCount(DataGridViewElementStates.Selected)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是或之间的区别是什么有什么区别:

What is the difference between or is there any difference between:

myDgv.SelectedRows.Count 

VS

myDgv.Rows.GetRowCount(DataGridViewElementStates.Selected)

在一个DataGridView,Windows窗体的背景?

in the context of a DataGridView, Windows Forms ?

推荐答案

我觉得你在这里提到的具体情况下,有从订单这两个选项之间的差异很小一边是其中的数据您DataGridView的将被处理。如果你保存了您使用,以获得从伯爵你可能会担心SelectedRows是展示您与该参考了当时的静态快照中间对象,但因为这两个选项都直接调用另一种方法,不该'吨真的在这里的一个因素。

I think in the specific case you mention here there is very little difference between these two options aside from the order in which the data of your DataGridView will be processed. If you were saving off the intermediate objects that you're using to get the Count from you might be concerned that SelectedRows is presenting you with a static snapshot at the time that the reference is made but since both options are directly calling another method that shouldn't really be a factor here.

当你真正好奇如何通过像这样挖就可以弹出打开ILDASM并浏览到您的GAC看到来电是如何工作的。

Whenever you're really curious about how to dig through something like this you can pop open ILDASM and browse into your GAC to see how the calls actually work.

这是一个有点高层次的使用SelectedRows和Rows.GetCount()之间的区别是,我们要么得到一个过滤收集并检查其大小或获取整个收集和过滤下来到一个子集的大小,我们再取回。这是由什么我们对初次使用IL向我们展示了非常诞生出来。

From a somewhat high-level the distinction between using SelectedRows and Rows.GetCount() is that we're either getting a filtered collection and checking its size or getting the entire collection and filtering it down to a subset whose size we then retrieve. This is pretty much born out by what our IL for the initial usage shows us.

.method public hidebysig static void  testDG() cil managed
{
  // Code size       34 (0x22)
  .maxstack  2
  .locals init ([0] class [System.Windows.Forms]System.Windows.Forms.DataGridView dgvTest,
           [1] int32 myNum,
           [2] int32 otherum)
  IL_0000:  nop
  IL_0001:  newobj     instance void [System.Windows.Forms]System.Windows.Forms.DataGridView::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance class [System.Windows.Forms]System.Windows.Forms.DataGridViewSelectedRowCollection [System.Windows.Forms]System.Windows.Forms.DataGridView::get_SelectedRows()
  IL_000d:  callvirt   instance int32 [System.Windows.Forms]System.Windows.Forms.BaseCollection::get_Count()
  IL_0012:  stloc.1
  IL_0013:  ldloc.0
  IL_0014:  callvirt   instance class [System.Windows.Forms]System.Windows.Forms.DataGridViewRowCollection [System.Windows.Forms]System.Windows.Forms.DataGridView::get_Rows()
  IL_0019:  ldc.i4.s   32
  IL_001b:  callvirt   instance int32 [System.Windows.Forms]System.Windows.Forms.DataGridViewRowCollection::GetRowCount(valuetype [System.Windows.Forms]System.Windows.Forms.DataGridViewElementStates)
  IL_0020:  stloc.2
  IL_0021:  ret
} // end of method Program::testDG

正如你可能犯罪嫌疑人,虽然这种回避了什么这两个低级通话做些什么的问题。

As you likely suspect though this kind of begs the question of what these two lower-level calls do.

在这种情况下,在DataGridView中的行属性创建的DataGridViewsRowCollection的一个实例,因为其映射到get_Rows传递回来。

In this case the Rows property on the DataGridView creates an instance of the DataGridViewsRowCollection and passes it back since it maps to get_Rows.

.method public hidebysig specialname instance class System.Windows.Forms.DataGridViewRowCollection 
        get_Rows() cil managed
{
  // Code size       27 (0x1b)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldfld      class System.Windows.Forms.DataGridViewRowCollection System.Windows.Forms.DataGridView::dataGridViewRows
  IL_0006:  brtrue.s   IL_0014
  IL_0008:  ldarg.0
  IL_0009:  ldarg.0
  IL_000a:  callvirt   instance class System.Windows.Forms.DataGridViewRowCollection System.Windows.Forms.DataGridView::CreateRowsInstance()
  IL_000f:  stfld      class System.Windows.Forms.DataGridViewRowCollection System.Windows.Forms.DataGridView::dataGridViewRows
  IL_0014:  ldarg.0
  IL_0015:  ldfld      class System.Windows.Forms.DataGridViewRowCollection System.Windows.Forms.DataGridView::dataGridViewRows
  IL_001a:  ret
} // end of method DataGridView::get_Rows

当我们看在DataGridView中SelectedRows属性我们可以看到,它更最初做了很多不仅仅是返回一个集合,但在主try块我们看到我们的get_Rows再次调用(IL_0045)。

When we look at the SelectedRows property on the DataGridView we see that it's doing a lot more initially than just returning a collection but in the main try block we see our "get_Rows" call again (IL_0045).

.try
  {
    IL_0035:  br.s       IL_0056
    IL_0037:  ldloc.3
    IL_0038:  callvirt   instance object [mscorlib]System.Collections.IEnumerator::get_Current()
    IL_003d:  unbox.any  [mscorlib]System.Int32
    IL_0042:  stloc.1
    IL_0043:  ldloc.0
    IL_0044:  ldarg.0
    IL_0045:  call       instance class System.Windows.Forms.DataGridViewRowCollection System.Windows.Forms.DataGridView::get_Rows()
    IL_004a:  ldloc.1
    IL_004b:  callvirt   instance class System.Windows.Forms.DataGridViewRow System.Windows.Forms.DataGridViewRowCollection::get_Item(int32)
    IL_0050:  callvirt   instance int32 System.Windows.Forms.DataGridViewSelectedRowCollection::Add(class System.Windows.Forms.DataGridViewRow)
    IL_0055:  pop
    IL_0056:  ldloc.3
    IL_0057:  callvirt   instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
    IL_005c:  brtrue.s   IL_0037
    IL_005e:  leave.s    IL_0074
  }  // end .try

这表明,我们正在做同样的检索和过滤操作为我们两个选择,而且做他们的性价比,可以合理预期的是相同的。

This suggests that we're doing the same retrieval and filtering operations for our two choices and that the "cost" of doing them could be reasonably expected to be the same.

如果你注意到身边这些电话的性能问题,你可以运行一些测试,看看是否是有区别的,但基于对IL我会感到惊讶,如果你发现有任何在两个电话之间明显的差别。

If you're noting performance issues around these calls you could run some tests to see if there is a difference but based on the IL I would be surprised if you found any appreciable distinction between the two calls

这篇关于SelectedRows.Count VS Rows.GetRowCount(DataGridViewElementStates.Selected)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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