获取datagrid绑定不同的值C#WPF [英] Getting datagrid binding distinct values C# WPF

查看:74
本文介绍了获取datagrid绑定不同的值C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的C#代码。



我有一个datagrid绑定,它收集应用程序的名称,并给我应用程序和评估状态的名称。



我正在尝试获得唯一数量的评估状态。评估状态将是一个数字。

我能够获得总行数,但我无法获得不同值的数量并将它们放在标签中。



第1栏|第2栏(评估状态)

浏览器| 1

办公室申请| 1

计算器| 2

记事本| 1



例如:



总eval状态1 = ??

总eval状态2 = ??







试试
{
ManagementScope scope = new ManagementScope(@\\+ TargetComputerName.Text + @\ ROOT \ccm \ClientSDK);
SelectQuery query = new SelectQuery(Select * From CCM_Application);
var typeDescriptors = new ObservableCollection< ManagementObjectTypeDescriptor>();

using(var searcher = new ManagementObjectSearcher(scope,query))
{
using(var managementObjects = searcher.Get())
{
foreach(managementObjects中的ManagementBaseObject managementObject)
{
using(managementObject)
{
typeDescriptors.Add(new ManagementObjectTypeDescriptor(managementObject));
}
}
}
}
this.Dispatcher.Invoke((行动)(()=>

CcmAppsGrid.ItemsSource = new ArrayList(typeDescriptors)));
CcmAppsGrid.Items.SortDescriptions.Clear();
CcmAppsGrid.Items.SortDescriptions.Add(new SortDescription(Name,ListSortDirection.Ascending));
int i = CcmAppsGrid.Items.Count;
lblTotal.Content =集合中的应用总数:+ i;
}
catch(Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}





我尝试过:



< blockquote  class  =   quote>< div  class  =    op> Quote:< /   div  >   int  count = CcmAppsGrid.SelectAll()。其中​​(s = < span class =code-keyword>>  s [  EvaluationState]。 ToString()。ToUpper()==   1)。Count(); 
this .lblinstalled.Content = count.ToString(); < / blockquote >

解决方案

< blockquote>分组和计数:



c# - 按列表分组并计入列表 - 堆栈溢出 [ ^ ]


Here is my C# code.

I have a datagrid binding which gathers name of applications and gives me the name of the application and evaluation state.

I am trying to get the unique number of Evaluation states. Evaluation state will be a number.
I am able to get the total number of rows but I am unable to get the number of distinct values and put them in a label.

Column 1 | Column 2(evaluationstate)
browser | 1
office application | 1
calculator | 2
notepad | 1

example:

total eval state 1 = ??
total eval state 2 = ??



try
          {
              ManagementScope scope = new ManagementScope(@"\\" + TargetComputerName.Text + @"\ROOT\ccm\ClientSDK");
              SelectQuery query = new SelectQuery("Select * From CCM_Application");
              var typeDescriptors = new ObservableCollection<ManagementObjectTypeDescriptor>();

              using (var searcher = new ManagementObjectSearcher(scope, query))
              {
                  using (var managementObjects = searcher.Get())
                  {
                      foreach (ManagementBaseObject managementObject in managementObjects)
                      {
                          using (managementObject)
                          {
                              typeDescriptors.Add(new ManagementObjectTypeDescriptor(managementObject));
                          }
                      }
                  }
              }
              this.Dispatcher.Invoke((Action)(() =>

                  CcmAppsGrid.ItemsSource = new ArrayList(typeDescriptors)));
                   CcmAppsGrid.Items.SortDescriptions.Clear();
                  CcmAppsGrid.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
                  int i = CcmAppsGrid.Items.Count;
                  lblTotal.Content = "Total Apps in collection: " + i;
                                         }
          catch (Exception ex)
          {
              System.Windows.MessageBox.Show(ex.Message);
          }



What I have tried:

<blockquote class="quote"><div class="op">Quote:</div>                       int count = CcmAppsGrid.SelectAll().Where(s => s["EvaluationState"].ToString().ToUpper() == "1").Count();
                     this.lblinstalled.Content = count.ToString();</blockquote>

解决方案

Group by and count:

c# - Group by and count in List - Stack Overflow[^]


这篇关于获取datagrid绑定不同的值C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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