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

C# DataGridView自动填充数据及计算总价功能实现求助

解决DataGridView自动填充与合计计算的问题

嘿,我来帮你搞定剩下的功能!既然你已经实现了Item_desc的自动补全,那接下来的自动填充和合计计算其实很直观,咱们一步步拆解:

一、输入Item_desc后自动填充Item_code和Item_rate

你需要监听DataGridView的单元格离开事件(比如CellLeave或Validating),当用户在Item_desc单元格输入完成并移开焦点时,根据输入的描述去你的数据源(数据库、本地集合等)查询对应的编码和单价,然后自动填充到同行的Item_code和Item_rate单元格。

举个C# WinForms的示例代码(假设你的数据源是一个List<Item>集合,Item类包含Code、Desc、Rate属性):

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    // 确保当前操作的是Item_desc列(根据你的实际列顺序调整索引)
    if (e.ColumnIndex == dataGridView1.Columns["Item_desc"].Index && e.RowIndex >= 0)
    {
        // 获取用户输入的Item_desc内容
        string inputDesc = dataGridView1.Rows[e.RowIndex].Cells["Item_desc"].Value?.ToString().Trim();
        
        if (!string.IsNullOrEmpty(inputDesc))
        {
            // 从数据源查询匹配的Item(替换成你实际的查询逻辑,比如数据库查询)
            var matchedItem = itemList.FirstOrDefault(item => item.Desc.Equals(inputDesc, StringComparison.OrdinalIgnoreCase));
            
            if (matchedItem != null)
            {
                // 自动填充Item_code和Item_rate
                dataGridView1.Rows[e.RowIndex].Cells["Item_code"].Value = matchedItem.Code;
                dataGridView1.Rows[e.RowIndex].Cells["Item_rate"].Value = matchedItem.Rate;
                
                // 顺便触发合计计算,避免用户还要手动输入数量再计算
                CalculateTotalForRow(dataGridView1.Rows[e.RowIndex]);
            }
            else
            {
                // 没找到匹配项时清空对应单元格
                dataGridView1.Rows[e.RowIndex].Cells["Item_code"].Value = null;
                dataGridView1.Rows[e.RowIndex].Cells["Item_rate"].Value = null;
            }
        }
    }
}

二、输入Item_qty后自动计算Item_total

这一步需要监听CellValueChanged事件,当Item_qty单元格的值发生变化时,获取同行的Item_rate和Item_qty,计算乘积后赋值给Item_total。记得处理空值和非数字的情况,避免程序报错。

示例代码:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // 确保当前操作的是Item_qty列(根据实际列顺序调整索引)
    if (e.ColumnIndex == dataGridView1.Columns["Item_qty"].Index && e.RowIndex >= 0)
    {
        CalculateTotalForRow(dataGridView1.Rows[e.RowIndex]);
    }
}

// 封装合计计算方法,方便复用
private void CalculateTotalForRow(DataGridViewRow row)
{
    // 尝试解析单价和数量,避免格式错误导致崩溃
    if (decimal.TryParse(row.Cells["Item_rate"].Value?.ToString(), out decimal rate) &&
        int.TryParse(row.Cells["Item_qty"].Value?.ToString(), out int qty))
    {
        // 计算总价并赋值
        row.Cells["Item_total"].Value = rate * qty;
    }
    else
    {
        // 单价或数量无效时清空总价
        row.Cells["Item_total"].Value = null;
    }
}

额外注意事项

  • 记得在窗体构造函数或Load事件里绑定这些事件:
    dataGridView1.CellLeave += dataGridView1_CellLeave;
    dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;
    
  • 如果自动补全是下拉列表形式,也可以在下拉项的SelectedIndexChanged事件里直接触发填充,这样用户选择补全项时就立刻填充,不用等离开单元格;
  • 若数据源是数据库,建议用异步查询避免UI卡顿。

内容的提问来源于stack exchange,提问作者Sijo George Oommen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:57:13