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;}//保存数据。。。。}}
}