首页 > 解决方案 > 我有两个datagridview,一个在员工电话上,我想让另一个包含打电话的员工人数

问题描述

下图显示了我的两个数据网格视图 数据网格视图

标签: c#c#-4.0c#-3.0

解决方案


根据我对您的问题的了解,您可以使用以下方法

      public DataTable numOfCalls(DataTable table)
    {
        //Initialize Result Table
        DataTable numOfCallsTable = new DataTable();
        numOfCallsTable.Columns.Add("agentName", typeof(string));
        numOfCallsTable.Columns.Add("numOfCall", typeof(int));

        // Get the unique agents
        DataView view = new DataView(table);
        DataTable distinctValues = view.ToTable(true, "agentName");

        // Check if there are agents
        if (distinctValues.Rows.Count > 0)
        {
            // Loop thru the agents
            for (int i = 0; i < distinctValues.Rows.Count; i++)
            {
                string agentName = distinctValues.Rows[i]["agentName"].ToString();
                // Get all the rows that this agent has
                DataRow[] agentRows = table.Select($"agentName='{agentName}'");
                // And then fill the second table
                DataRow newRow = numOfCallsTable.NewRow();
                newRow["agentName"] = agentName;
                newRow["numOfCall"] = agentRows.Length;
                numOfCallsTable.Rows.Add(newRow);
            }
        }
        return numOfCallsTable;
    }

每次向第一个表添加或删除行时,都可以调用此方法。

更新您询问的按钮

        private void Button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)dataGridView1.DataSource;

            dataGridView2.DataSource = numOfCalls(dt);

        }

推荐阅读