You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataGridView合并特定单元格:实现按人员汇总付款金额需求

实现方案

针对你两个窗体的付款数据展示需求,按以下步骤实现即可:

一、先做数据分组累计,确保每人一行

每次Form1保存付款数据后,不要直接往Form2的DataGridView追加行,而是先对所有付款记录按人员分组,计算累计收款金额,再把汇总结果作为数据源绑定到DataGridView。

示例代码(假设用实体类存储付款数据)

先定义付款数据模型:

public class PaymentRecord
{
    public string PersonName { get; set; } // 人员姓名
    public decimal GetMoney { get; set; } // 单次收款金额
    public string Department { get; set; } // 人员所属部门(示例其他字段)
}

在Form2中编写汇总逻辑:

// 获取所有历史付款记录(从数据库/本地存储读取)
private List<PaymentRecord> GetAllPayments()
{
    // 这里替换成你实际的读取逻辑,比如从数据库查询
    return new List<PaymentRecord>();
}

// 更新DataGridView数据
public void RefreshPaymentSummary()
{
    var allPayments = GetAllPayments();
    // 按人员分组,计算累计收款
    var summaryList = allPayments
        .GroupBy(p => p.PersonName)
        .Select(g => new
        {
            人员姓名 = g.Key,
            累计收款 = g.Sum(p => p.GetMoney),
            所属部门 = g.First().Department // 取该人员的部门信息
        })
        .ToList();
    // 绑定到DataGridView
    dataGridView1.DataSource = summaryList;
}

二、实现特定单元格合并

如果需要合并人员信息类的重复单元格(比如同一人员的「所属部门」列),通过DataGridView的CellPainting事件来处理:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    // 指定需要合并的列索引(比如人员姓名列是0,所属部门列是2)
    int[] mergeColumnIndexes = { 0, 2 };
    if (mergeColumnIndexes.Contains(e.ColumnIndex) && e.RowIndex >= 0)
    {
        // 隐藏底部边框
        e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;

        // 和上一行单元格值相同的话,隐藏顶部边框
        if (e.RowIndex > 0)
        {
            var currentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            var prevCell = dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex];
            if (currentCell.Value != null && currentCell.Value.Equals(prevCell.Value))
            {
                e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
            }
            else
            {
                e.AdvancedBorderStyle.Top = dataGridView1.AdvancedCellBorderStyle.Top;
            }
        }
        // 绘制单元格内容
        e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
        e.Handled = true;
    }
}

三、Form1保存后联动更新Form2

在Form1的保存按钮事件里,完成数据存储后,调用Form2的刷新方法:

private void btnSave_Click(object sender, EventArgs e)
{
    // 1. 保存当前月度付款记录到存储介质
    var newPayment = new PaymentRecord
    {
        PersonName = txtPerson.Text,
        GetMoney = decimal.Parse(txtMoney.Text),
        Department = txtDept.Text
    };
    SaveNewPayment(newPayment); // 替换成你的保存逻辑

    // 2. 刷新Form2的数据
    var form2 = Application.OpenForms.OfType<Form2>().FirstOrDefault();
    if (form2 != null)
    {
        form2.RefreshPaymentSummary();
    }
}

注意事项

  • 确保分组前的付款记录是按人员排序的,否则同一人员的行不连续,单元格合并会失效
  • 如果使用DataTable作为数据源,可改用LINQ to DataSet实现分组汇总
  • DataGridView的列可以提前在设计器中配置好绑定字段,避免自动生成列的混乱

内容的提问来源于stack exchange,提问作者user25615183

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:32:12