当前位置: 首页 > news >正文

winform表格DataGridView多个单元格批量输入数字

winform表格DataGridView多个单元格批量输入数字

DataGrid单元格批量输入

using BaseControls;
using BaseControls.HLGridViewCustomClass;
using FrmClassLibraryTool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;namespace HFPersonFrmMESProcessingVerification
{/// <summary>/// 生产工序记录核定工时/// </summary>/// <remarks>/// 创建时间:2025-7-16 10:15:24,作者:王浩力/// </remarks>public partial class FrmMESProcessingVerification : Form{/// <summary>/// 输入数字的文本框/// </summary>TextBox _txtNumberInput = new TextBox();System.Timers.Timer timer = null;/// <summary>/// 批量输入的单元格/// </summary>static List<DataGridViewCell> selectedCells = new List<DataGridViewCell>();public FrmMESProcessingVerification(){InitializeComponent();this.Controls.Add(_txtNumberInput);_txtNumberInput.BringToFront();_txtNumberInput.Visible = false;this.Load += FrmMESProcessingVerification_Load;this.dataGridViewProcessingVerification.CellMouseUp += DataGridViewProcessingVerification_CellMouseUp; ;this.dataGridViewProcessingVerification.Scroll += (s, e) =>{_txtNumberInput.Visible = false;};}//单元格批量输入private void DataGridViewProcessingVerification_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e){var dgvdate = sender as HLDataGridView;if (e.RowIndex < 0 && e.ColumnIndex < 0 && e.Button != MouseButtons.Left){return;}var _syncContext = SynchronizationContext.Current;Rectangle _rect = this.RectangleToClient(dgvdate.RectangleToScreen(dgvdate.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true)));if (e.ColumnIndex == dgvdate.Columns["approvalWorktime"].Index && dgvdate.SelectedCells.Count > 1){_txtNumberInput.Location = _rect.Location;_txtNumberInput.Size = _rect.Size;_txtNumberInput.Visible = true;_txtNumberInput.Focus();_txtNumberInput.SelectAll();_txtNumberInput.KeyUp += (s1, e1) =>{var textBoxK = s1 as TextBox;System.Diagnostics.Debug.WriteLine("输入值:" + textBoxK.Text);if (Regex.IsMatch(textBoxK.Text, @"[A-Za-z]+")){System.Diagnostics.Debug.WriteLine("不是数字,输入值:" + textBoxK.Text);textBoxK.Text = "";e1.Handled = true;}if (!Regex.IsMatch(textBoxK.Text, @"(^\d+\.{0,1}\d+|\d+)")){System.Diagnostics.Debug.WriteLine("不是数字,输入值:" + textBoxK.Text);textBoxK.Text = "";e1.Handled = true;return;}selectedCells.Clear();if (e1.KeyCode == Keys.Escape){_txtNumberInput.Visible = false;return;}var inputValue2 = _txtNumberInput.Text.Trim();System.Diagnostics.Debug.WriteLine("当前输入值2:" + inputValue2);//选中的单元格赋值foreach (DataGridViewCell item in dgvdate.SelectedCells){item.Value = inputValue2;selectedCells.Add(item);}if (timer != null){timer.Stop();timer.Close();}timer = new System.Timers.Timer(1500);timer.Elapsed += (s2, e2) =>{_syncContext.Post(a =>{_txtNumberInput.Visible = false;}, null);timer.Stop();};timer.Start();};_txtNumberInput.LostFocus += (g, e2) =>{_txtNumberInput.Visible = false;};}else{}//var inputValue = this.dataGridViewProcessingVerification.CurrentCell.Value;//System.Diagnostics.Debug.WriteLine("当前输入值:" + inputValue);}private void dataGridViewProcessingVerification_CellParsing(object sender, DataGridViewCellParsingEventArgs e){var dgvdate = sender as HLDataGridView;if (e.ColumnIndex == dgvdate.Columns["approvalWorktime"].Index){selectedCells.Clear();if (decimal.TryParse(e.Value.ToString(),out decimal resultValue)){if (resultValue > 0){selectedCells.Add(dgvdate.CurrentCell);}}}}private void FrmMESProcessingVerification_Load(object sender, EventArgs e){this.dataGridViewProcessingVerification.AutoGenerateColumns = false;dateTimePickerBaoG_Start.Value = DateTime.Now.AddDays(-30); dateTimePickerBaoG_Start.Checked = false;cmbStat.Text = "待核定";comboBoxDateType.Text = "报工日期";//设置列HLDataGridViewTool.LoadHLDataGridViewColumnXmlConfigSet(this.dataGridViewProcessingVerification);GetPage();}/// <summary>/// 分页查询/// </summary>void GetPage(){          }/// <summary>/// 查询/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnSel_Click(object sender, EventArgs e){GetPage();}/// <summary>/// 核定,保存/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnPricing_Click(object sender, EventArgs e){//提交编辑数据this.dataGridViewProcessingVerification.EndEdit();//保存批量输入的核定工时if (selectedCells.Count == 0){MessageBox.Show("请至少选择一条记录进行核定!");return;}//保存数据。。。。}}
}
http://www.lryc.cn/news/599310.html

相关文章:

  • 瑞萨电子RA-T MCU系列新成员RA2T1——电机控制专家
  • MySQL性能优化配置终极指南
  • 详谈OSI七层模型和TCP/IP四层模型以及tcp与udp为什么是4层,http与https为什么是7层
  • Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
  • STM32与ADS1220实现多通道数据采集的完整分析和源程序
  • 【WPS】office邮件合并,怎么将数据源excel中的下一条拼接在文档中的下一个位置
  • 目标导向的强化学习:问题定义与 HER 算法详解—强化学习(19)
  • Android Kotlin 协程全面指南
  • C++ : list的模拟
  • 【数据结构】长幼有序:树、二叉树、堆与TOP-K问题的层次解析(含源码)
  • 安全风险监测平台:被动应对向主动预防的转变
  • Nginx 安装与 HTTPS 配置指南:使用 OpenSSL 搭建安全 Web 服务器
  • 【IDEA】idea怎么修改注册的用户名称?
  • OAuth 2.0 安全最佳实践 (RFC 9700) password 授权类型已经不推荐使用了,将在计划中移除
  • 关于新学C++编程Visual Studio 2022开始,使用Cmake工具构建Opencv和SDK在VS里编译项目开发简介笔记
  • 《汇编语言:基于X86处理器》第9章 编程练习
  • vscode 登录ssh记住密码直接登录设置
  • vscode 字体的跟换
  • Web前端:JavaScript Math选字游戏 斯特鲁普效应测试
  • 短剧广告变现系统全栈开发指南:从架构设计到高并发实践
  • 动态规划Day1学习心得
  • RocketMQ常见问题梳理
  • kafka如何保证数据不丢失
  • 2025年7月25日训练日志
  • Elasticsearch-8.17.0 centos7安装
  • Flink 自定义类加载器和子优先类加载策略
  • 第一章:Go语言基础入门之流程控制
  • k8s-MongoDB 副本集部署
  • 呼叫中心系统管理权限功能配置
  • gig-gitignore工具实战开发(四):使用ai辅助生成gitignore