c# - 我有两个datagridview,一个在员工电话上,我想让另一个包含打电话的员工人数
问题描述
下图显示了我的两个数据网格视图
解决方案
根据我对您的问题的了解,您可以使用以下方法
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);
}
推荐阅读
- java - How do I go down a binary tree using questions?
- android - 错误:无法解决:com.google.android.support:wearable:2.0.0
- git - .gitignore 文件是否适用于所有分支?
- reactjs - Heroku 使用第三方凭据部署
- java - JUNIT 测试断言
- python - Importing .yml file to anaconda Error: "Solving environment: Failed. "
- javascript - get key name of item being iterated on forEach loop
- c# - 如何制作游戏中的赌博脚本
- chisel - Chisel 中使用 MuxCase 的示例
- r - R函数将数据框剥离成单独的列poly