gpt4 book ai didi

c# - DataGridView -- 同时输入输出 -- 这是DataGridView的bug吗

转载 作者:太空狗 更新时间:2023-10-29 23:48:48 25 4
gpt4 key购买 nike

我创建了一个 C# Windows 窗体应用程序,我试图尽可能简单地演示我遇到的问题。我正在尝试使用 DataGridView 允许用户在一列中输入,同时从后台线程获取另一列中的更新。

问题是输入列实际上是不可编辑的,因为——我认为——针对输出列的更新会导致输入列在用户试图更改它时用它的当前值更新.

这是 DataGridView 中的错误吗?有没有更好的方法来做这种事情?谁能推荐一个好的解决方法?

下面的代码演示了这个问题。输出列将不断更新,输入列几乎不可编辑。我已将设计器代码 (Form1.designer.cs) 和 Main(来自 Program.cs)合并到表单代码 (Form1.cs) 中——因此以下代码应该可以独立运行。

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Timers;

public partial class Form1 : Form
{
private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.SuspendLayout();
//
// dataGridView
//
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Location = new System.Drawing.Point(3, 12);
this.dataGridView.Name = "dataGridView";
this.dataGridView.RowTemplate.Height = 24;
this.dataGridView.Size = new System.Drawing.Size(322, 158);
this.dataGridView.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(328, 174);
this.Controls.Add(this.dataGridView);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView;

public Form1()
{
InitializeComponent();
}

BindingSource bindingSource = new BindingSource();
BindingList<Item> items = new BindingList<Item>();
private System.Timers.Timer timer;
private void Form1_Load(object sender, EventArgs e)
{
dataGridView.DataSource = bindingSource;
bindingSource.DataSource = items;
items.Add(new Item(dataGridView));

timer = new System.Timers.Timer {Interval = 50};
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}

private Random random = new Random();
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
items[0].SetOutput(random.Next(100));
}
}


class Item : INotifyPropertyChanged
{
public int Input { get; set; }

private int output;
public int Output
{
get { return output; }
private set
{
output = value;
OnPropertyChanged("Output");
}
}

public Control control;

public Item(Control control)
{
this.control = control;
}

public void SetOutput(int outputValue)
{
Output = outputValue;
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
if(!control.IsDisposed)
control.BeginInvoke(handler, this, new PropertyChangedEventArgs(name));
}
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

最佳答案

我怀疑当 PropertyChanged 事件发生时,DataGridView 会刷新所有单元格,或者可能只刷新行中发生更改的单元格(当您编辑时会发生这种情况吗?另一行?),丢失所有未提交的更改。

如果您可以在 DataGridView 刷新单元格之前拦截事件,则可以保存未提交的更改,以便在刷新后恢复它们。但这将是一个丑陋的解决方法......

您是否在 MSDN 论坛上提问过?也许来自 MS 的人可以给你一个更有用的答案

关于c# - DataGridView -- 同时输入输出 -- 这是DataGridView的bug吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1942782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com