Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SelectedRows.Count vs Rows.GetRowCount(DataGridViewElementState.Selected)_C#_.net_Winforms_Datagridview - Fatal编程技术网

C# SelectedRows.Count vs Rows.GetRowCount(DataGridViewElementState.Selected)

C# SelectedRows.Count vs Rows.GetRowCount(DataGridViewElementState.Selected),c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,以下两者之间有什么区别或有什么区别: myDgv.SelectedRows.Count vs 在DataGridView的上下文中,Windows窗体?我认为在您在这里提到的特定情况下,除了处理DataGridView数据的顺序之外,这两个选项之间几乎没有什么区别。如果您正在保存用于从中获取计数的中间对象,您可能会担心SelectedRows在进行引用时会向您显示一个静态快照,但由于这两个选项都直接调用另一个方法,因此在这里不应成为真正的因素 每当你真的对如何挖掘这样的东西感到好奇时,你可以

以下两者之间有什么区别或有什么区别:

myDgv.SelectedRows.Count 
vs


在DataGridView的上下文中,Windows窗体?

我认为在您在这里提到的特定情况下,除了处理DataGridView数据的顺序之外,这两个选项之间几乎没有什么区别。如果您正在保存用于从中获取计数的中间对象,您可能会担心SelectedRows在进行引用时会向您显示一个静态快照,但由于这两个选项都直接调用另一个方法,因此在这里不应成为真正的因素

每当你真的对如何挖掘这样的东西感到好奇时,你可以打开ILDASM并浏览你的GAC,看看这些调用是如何工作的

从某种程度上讲,使用SelectedRows和Rows.GetCount()之间的区别在于,我们要么获取一个已过滤的集合并检查其大小,要么获取整个集合并将其过滤到一个子集,然后检索该子集的大小。这在很大程度上是由我们最初使用的IL显示出来的

.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
正如您可能怀疑的那样,这类问题引出了这两个较低级别调用的作用

在本例中,DataGridView上的Rows属性创建DataGridViewsRowCollection的一个实例,并将其传回,因为它映射到get_行

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

这表明我们正在为两个选择执行相同的检索和过滤操作,并且可以合理地预期执行这些操作的“成本”是相同的


如果您注意到这些调用的性能问题,您可以运行一些测试来查看是否存在差异,但是基于IL,如果您发现两个调用之间有任何明显的差异,我会感到惊讶表明
SelectedCells
以及在较小程度上
SelectedRows
对于大数据来说效率低下设置,您应该使用
GetCellCount
/
GetRowCount

亲爱的Carth,感谢您的详细回答。谢谢。正如你所说,我将运行一些测试,看看是否有任何差异,并尽快公布我的结果。再次感谢!:)@很乐意帮忙。跟进这一点很有趣。
.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
.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