简体   繁体   English

当 DataGridView 绑定到 DataTable 时,如何在编辑 DataGridView 单元格后更新 object 数据?

[英]How do I update object data after editing a DataGridView cell when the DataGridView is bound to a DataTable?

first time poster.第一次海报。 Sorry if the code I post is poorly formatted or hard to interpret.抱歉,如果我发布的代码格式不正确或难以解释。

I am trying to figure out how to update the object property data I have in a class object "AnalogInput" located in a DataGridView that has it's DataSource property set to a DataTable.我试图弄清楚如何更新我在 class object “AnalogInput”中的 object 属性数据,该数据源属性设置为 DataGridView。 So, after I edit a cell in the DataGridView, I would like to send that updated value back to the DataTable and thus the AnalogInput property value.因此,在我编辑 DataGridView 中的单元格后,我想将该更新后的值发送回 DataTable,从而将 AnalogInput 属性值发送回。

I am trying to do this in a WinForms application.我正在尝试在 WinForms 应用程序中执行此操作。

Below is some code that sums up pretty much where I am at right now:下面是一些代码,总结了我现在所处的位置:

Create a DataGridView to display some select and editable AnalogInput properties创建一个 DataGridView 以显示一些 select 和可编辑的 AnalogInput 属性

DataGridView dataGridView_AnalogPoints = new dataGridView(); 

Create a new instance of a class object "AnalogInput" with public string properties initialized as shown below创建一个 class object "AnalogInput" 的新实例,并初始化公共字符串属性,如下所示

AnalogInput point = new AnalogInput() { Name = "Something", Address = "2", Minimum = "-32768", Maximum = "32767"};

Create a new DataTable to hold the desired properties of the object "AnalogInput"创建一个新的数据表来保存 object“AnalogInput”的所需属性

DataTable analogPoints = new DataTable();

Add the columns to the DataTable将列添加到 DataTable

analogPoints.Columns.Add("Name");
analogPoints.Columns.Add("Address");
analogPoints.Columns.Add("Minimum");
analogPoints.Columns.Add("Maximum");

Add the object data into a new row in the DataTable analogPoints将 object 数据添加到 DataTable 模拟点的新行中

analogPoints.Rows.Add(point.Name, point.Address, point.Minimum, point.Maximum);

Set the source of the dataGridView.设置dataGridView的来源。

dataGridView_AnalogPoints.DataSource = analogPoints;

So, once I edit the DataGridView cell that contains the object data from the DataSource as DataTable, which gets it's data from the AnalogInput object, how can I update it to the newly entered value from the DataGridView?因此,一旦我将包含来自 DataSource 的 object 数据的 DataGridView 单元格编辑为 DataTable,它从 AnalogInput object 中获取数据,如何将其更新为来自 DataGridView 的新输入值? I feel like I'm pretty far off from achieving this.我觉得我离实现这个目标还很远。

I only have about 9 months worth of experience working with C# and WinForms so I apologize if my code looks messy.我只有大约 9 个月的使用 C# 和 WinForms 的经验,所以如果我的代码看起来很乱,我深表歉意。 I'm also not sure what I should be searching for specifically to solve this.我也不确定我应该专门寻找什么来解决这个问题。

I don't think you are very far from achieving your intended behaviour.我认为你离实现你的预期行为并不遥远。 The DataGridView class has a wonderful event called CellValueChanged. DataGridView class 有一个很棒的事件,叫做 CellValueChanged。 In case you don't know what events are, think of them like a case that a developer believes may happen (like value changed, control clicked, mouse hover etc.) that s/he wants to allow you to subscribe methods to be invoked whenever the case occurs.如果您不知道什么是事件,请将它们视为开发人员认为可能发生的情况(例如值更改、控件单击、鼠标 hover 等),她/他希望您订阅要调用的方法每当案件发生。 What you wish to do is run a function whenever a value of a cell was changed.您想要做的是在单元格的值发生更改时运行 function。 To subscribe a method to an event you need to write the following line of code:要订阅事件的方法,您需要编写以下代码行:

objectInstanceName.EventName += MethodName; // subscribes a method to be invoked when event occurs

This is not necessarily relevant for your case here, but if you no longer want a method that you have previously subscribed to be invoked, you can unsubscribe it with the following line of code:这不一定与您的情况相关,但如果您不再希望调用之前订阅的方法,您可以使用以下代码行取消订阅:

objectInstanceName.EventName -= MethodName; // unsubscribes a method to cancel it from being invoked when event occurs

You can also subscribe as many methods as you wish for an event.您还可以为事件订阅任意数量的方法。 Note that every event expects the methods subscribed to it have a specified return type, amount of parameters and their type (those are specified by a thing called a delegate).请注意,每个事件都希望订阅它的方法具有指定的返回类型、参数数量及其类型(这些由称为委托的事物指定)。

In order to create a method that matches the expected return type and parameters you can press TAB twice right after typing the += and visual studio will automatically generate a method for you, or write the method's name and then hover on it with the mouse, open the suggestions menu and click "Generate method".为了创建与预期返回类型和参数匹配的方法,您可以在键入 += 后立即按 TAB 两次,Visual Studio 将自动为您生成一个方法,或者用鼠标在其上写下方法的名称和 hover,打开建议菜单并单击“生成方法”。

The CellValueChanged event "wants" its subscribed methods to have the following parameters (their names don't matter, what matters is their order and type): CellValueChanged 事件“希望”其订阅的方法具有以下参数(它们的名称无关紧要,重要的是它们的顺序和类型):

object sender, DataGridViewCellEventArgs e

The parameter sender is the object caused the method to be invoked (ie your DataGridView) and e is of type DataGridViewCellEventArgs, which has some properties that'll be very helpful for you: RowIndex, and ColumnIndex.参数sender是 object 导致该方法被调用(即您的 DataGridView),并且e是 DataGridViewCellEventArgs 类型,它有一些对您很有帮助的属性:RowIndex 和 ColumnIndex。 Using those parameters you can simply update the values of your DataTable and also your actual AnalogPoint instance in case you assigned it to some object.使用这些参数,您可以简单地更新 DataTable 的值以及实际的 AnalogPoint 实例,以防您将其分配给某些 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编辑基础DataTable时推迟DataGridView更新 - Deferring DataGridView update when editing the underlying DataTable 如何在绑定到数据表的Windows窗体dataGridView中显示时间 - how do I display time in a windows forms dataGridView bound to a datatable 绑定数据后如何更新另一种形式的DataGridView的内容 - How to update the content of DataGridView present in another form after the data is bound 如何更新绑定到列表的预格式化DataGridView? - How do I update a pre-formatted DataGridView bound to a list? 如何在编辑值 c# 后更新 dataGridview? - How can i update dataGridview After editing values c#? 对DataGridView排序后,如何更新数据源以反映DataGridView的绑定? - When DataGridView is sorted, how do I update the data source to reflect the DataGridView's binding? 如何通过另一个线程更新绑定到datagridview的dataset.datatable - How to update dataset.datatable bound to datagridview via another thread 更改dataGridView中的单元格值后如何更新数据源中的更改? - How to update changes in the data source after changing the cell value in the dataGridView? 在C#中进行编辑时和编辑后如何设置datagridview单元格格式? - How to set datagridview cell format when editing and after editing in C#? 与绑定到Datagridview的DataTable更新有关的性能问题 - Performance issue on the update of a DataTable bound to a Datagridview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM