当前位置: 首页 > 编程语言 > C#

DataGridView-同时输入和输出-这是DataGridView中的一个错误Share

时间:2023-04-10 22:59:15 C#

可能是我遇到的问题的简单演示。我正在尝试使用DataGridView允许用户在一列中输入,同时从后台线程获取另一列中的更新。问题在于输入列实际上不可编辑,因为-我认为-输出列的更新会导致输入列在用户尝试更改时更新为当前值。这是DataGridView中的错误吗?有没有更好的方法来做这种事情?谁能推荐一个好的解决方法?下面的代码演示了这个问题。“输出”列会不断更新,“输入”列几乎不可编辑。我已将设计器代码(Form1.designer.cs)和Main(来自Program.cs)合并到表单代码(Form1.cs)中-因此以下代码应自行运行。使用系统;使用System.ComponentModel;使用System.Windows.Forms;使用系统定时器;publicpartialclassForm1:Form{privateSystem.ComponentModel.IContainercomponents=null;protectedoverridevoidDispose(booldisposing){if(disposing&&(components!=null)){components.Dispose();}base.Dispose(处理);}#regionWindows窗体设计器生成的代码//////设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。///privatevoidInitializeComponent(){this.dataGridView=newSystem.Windows.Forms.DataGridView();((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();这个.SuspendLayout();////dataGridView//this.dataGridView.ColumnHeadersHeightSizeMode=System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;this.dataGridView.Location=newSystem.Drawing.Point(3,12);this.dataGridView.Name="dataGridView";这个.dataGridVview.RowTemplate.Height=24;this.dataGridView.Size=newSystem.Drawing.Size(322,158);this.dataGridView.TabIndex=0;////Form1//this.AutoScaleDimensions=newSystem.Drawing.SizeF(8F,16F);this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.ClientSize=newSystem.Drawing.Size(328,174);this.Controls.Add(this.dataGridView);this.Name="Form1";this.Text="Form1";this.Load+=newSystem.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();这个.ResumeLayout(false);}#endregionprivateSystem.Windows.Forms.DataGridViewdataGridView;publicForm1(){InitializeComponent();}BindingSourcebindingSource=newBindingSource();BindingListitems=newBindingList();私有System.Timers.Timer计时器;privatevoidForm1_Load(objectsender,EventArgse){dataGridView.DataSource=bindingSource;bindingSource.DataSource=项目;items.Add(新项目(dataGridView));计时器=新System.Timers.Timer{Interval=50};timer.Elapsed+=newElapsedEventHandler(timer_Elapsed);定时器。开始();}privateRandomrandom=newRandom();voidtimer_Elapsed(objectsender,ElapsedEventArgse){items[0].SetOutput(random.Next(100));}}classItem:INotifyPropertyChanged{publicintInput{get;放;}私有整数输出;publicintOutput{get{返回输出;}私有集{输出=值;OnPropertyChanged("输出");}}公共控制控制;公共项目(控制控制){this.control=control;}publicvoidSetOutput(intoutputValue){Output=outputValue;}公共事件PropertyChangedEventHandlerPropertyChanged;protectedvoidOnPropertyChanged(stringname){PropertyChangedEventHandlerhandler=PropertyChanged;if(handler!=null){if(!control.IsDisposed)control.BeginInvoke(handler,this,newPropertyChangedEventArgs(name));}}}staticclassProgram{//////应用程序的主要入口点。///[状态读取]staticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);应用程序运行(新Form1());我怀疑当PropertyChanged事件发生时,DataGridView会刷新所有单元格,或者可能只刷新行中已更改的单元格(当您编辑另一行时会发生什么?),丢失所有未修改的更改如果您可以在DataGridView之前拦截事件刷新单元格,您可以保存未提交的更改以在刷新时显示,稍后恢复它们。但那将是一个丑陋的解决方法......你在MSDN论坛上问过吗?也许MS的某个人可以给你一个更有用的答案也许正在编辑的单元格正在失去焦点?也许每次更新另一个栏目时,存储您正在编辑的位置,并在更新后重新设置焦点?尝试不在50毫秒内更新列,而是在类似于intervale等于4000的情况下更新它可以更改另一列中的值,并且仍然可以在另一列上更改,如上所示是C#学习教程:DataGridView-同时输入和输出-这是DataGridView中的错误分享的内容如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: