如何在Windows应用销售模块的DataGridView中实现TextBox离开事件自动填充字段
嘿,我来帮你搞定这个DataGridView自动填充的需求!针对你Windows应用销售模块的场景,我整理了一套实用的实现方案,一步步来:
实现思路概述
核心逻辑就是监听itemcode列单元格的离开事件,当用户输入完商品编码并移开焦点时,触发数据库查询,把对应的barcode、itemname、rate等数据自动填充到当前行的对应列,quantity可以默认设为1或者留空让用户手动修改。
具体实现步骤
1. 绑定DataGridView的CellLeave事件
首先在窗体的代码里绑定CellLeave事件,判断当前离开的单元格是不是itemcode列(用列名判断比索引更稳妥,避免列顺序调整出问题):
private void dataGridViewSales_CellLeave(object sender, DataGridViewCellEventArgs e) { // 确认当前操作的是itemcode列 if (dataGridViewSales.Columns[e.ColumnIndex].Name == "itemcode") { // 获取用户输入的商品编码,做空值和空格处理 string itemCode = dataGridViewSales.Rows[e.RowIndex].Cells["itemcode"].Value?.ToString().Trim(); // 如果编码不为空,执行查询填充逻辑 if (!string.IsNullOrEmpty(itemCode)) { FillItemDetails(dataGridViewSales.Rows[e.RowIndex], itemCode); } } }
2. 编写数据库查询与填充方法
接下来写FillItemDetails方法,负责从mydatagrid数据库中拉取对应商品的数据,并填充到当前行:
private void FillItemDetails(DataGridViewRow targetRow, string itemCode) { // 替换成你的实际数据库连接字符串 string connString = "Server=你的服务器名;Database=mydatagrid;Integrated Security=True;"; string query = "SELECT barcode, itemname, rate FROM 商品表名 WHERE itemcode = @ItemCode"; // 使用using语句自动释放数据库资源 using (SqlConnection conn = new SqlConnection(connString)) { try { conn.Open(); SqlCommand cmd = new SqlCommand(query, conn); // 参数化查询,绝对避免SQL注入风险 cmd.Parameters.AddWithValue("@ItemCode", itemCode); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { // 填充各列数据 targetRow.Cells["barcode"].Value = reader["barcode"].ToString(); // 给ComboBox类型的itemname列设值(提前绑定数据源的话会自动匹配) targetRow.Cells["itemname"].Value = reader["itemname"].ToString(); targetRow.Cells["rate"].Value = reader["rate"].ToString(); // 默认给quantity设为1,也可以留空让用户输入 if (targetRow.Cells["quantity"].Value == DBNull.Value || string.IsNullOrEmpty(targetRow.Cells["quantity"].Value.ToString())) { targetRow.Cells["quantity"].Value = "1"; } } else { // 没找到对应商品的处理:提示用户并清空当前行其他列 MessageBox.Show($"未找到编码为{itemCode}的商品,请检查输入!"); targetRow.Cells["barcode"].Value = null; targetRow.Cells["itemname"].Value = null; targetRow.Cells["rate"].Value = null; targetRow.Cells["quantity"].Value = null; } reader.Close(); } catch (Exception ex) { MessageBox.Show($"查询商品数据出错:{ex.Message}"); } } }
3. 额外优化建议
- 提前绑定ComboBox数据源:如果
itemname是ComboBox列,建议在窗体加载时把所有商品名称绑定到该列的数据源,避免设置值时显示异常:private void SalesForm_Load(object sender, EventArgs e) { string connString = "你的数据库连接字符串"; string query = "SELECT DISTINCT itemname FROM 商品表名"; List<string> itemNames = new List<string>(); using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); SqlCommand cmd = new SqlCommand(query, conn); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { itemNames.Add(reader["itemname"].ToString()); } reader.Close(); } DataGridViewComboBoxColumn itemNameCol = (DataGridViewComboBoxColumn)dataGridViewSales.Columns["itemname"]; itemNameCol.DataSource = itemNames; } - 单元格验证:可以在
CellValidating事件里先验证itemcode的格式(比如是否为数字、长度是否符合要求),再执行查询。 - 内存缓存优化:如果商品数据量不大,建议提前把所有商品数据加载到内存中的
DataTable,每次查询直接从内存取,减少数据库访问次数,提升响应速度。 - 新行判断:如果DataGridView允许添加新行,要判断当前行是不是新行(
targetRow.IsNewRow),避免在新行未初始化时执行查询。
内容的提问来源于stack exchange,提问作者Khaleel R
相关产品推荐
相关产品推荐

