Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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# 在DataGridView中为分组值添加“计数”列_C#_Winforms_Sharepoint_Datagridview - Fatal编程技术网

C# 在DataGridView中为分组值添加“计数”列

C# 在DataGridView中为分组值添加“计数”列,c#,winforms,sharepoint,datagridview,C#,Winforms,Sharepoint,Datagridview,我正在从SharePoint列表中填充DataGridView,效果很好 我需要DataGridView中的一个新列,显示每个分组列表项的计数 到目前为止,我已经: if (collListItem.Count != 0) { var dtCoaching = new DataTable(); dtCoaching.Columns.AddRange(new[] { new DataColumn("Call Type"), new DataColumn("

我正在从SharePoint列表中填充DataGridView,效果很好

我需要DataGridView中的一个新列,显示每个分组列表项的计数

到目前为止,我已经:

if (collListItem.Count != 0)
{
    var dtCoaching = new DataTable();

    dtCoaching.Columns.AddRange(new[]
    {
       new DataColumn("Call Type"),  new DataColumn("Count")
    });

    foreach (var oListItem in collListItem)
    {
        dtCoaching.Rows.Add(oListItem["Call_x0020_Type"]);
    }

    dtCoaching = dtCoaching.AsEnumerable()
    .GroupBy(r => new { Col1 = r["Call Type"]})
    .Select(g => g.OrderBy(r => r["Call Type"]).First())
    .CopyToDataTable();

    if (dataGridViewCallType!= null)
    {
        dataGridViewCallType.DataSource = dtCoaching;
    }                   
}
我见过很多输出到控制台的解决方案,这很好,但我需要DataTable绑定到DataGridView,而不是输出到控制台

这是到目前为止的输出:

我只需要计数列就行了

谢谢


Davie找到了答案,需要克隆DataTable并使用Count列构建一个新的DataTable

            if (collListItem.Count != 0)
            {
                var dtCoaching = new DataTable();

                dtCoaching.Columns.AddRange(new[]
                {
                    new DataColumn("Call Type"),  new DataColumn("Count")
                });

                foreach (var oListItem in collListItem)
                {
                    dtCoaching.Rows.Add(oListItem["Call_x0020_Type"]);
                }

                var result = from row in dtCoaching.AsEnumerable()
                             group row by row.Field<string>("Call Type") into grp
                             select new
                             {
                                 CallType = grp.Key,
                                 CallCount = grp.Count()
                             };

                DataTable dtCoachingClone = new DataTable();
                dtCoachingClone = dtCoaching.Clone();

                foreach (var item in result)
                {
                    DataRow newRow = dtCoachingClone.NewRow();
                    newRow["Call Type"] = item.CallType;
                    newRow["Count"] = item.CallCount;
                    dtCoachingClone.Rows.Add(newRow);
                }

                if (dataGridViewCallType != null)
                {
                    dataGridViewCallType.DataSource = dtCoachingClone;
                }

                return;
            }