0
votes

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. 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.

I am trying to do this in a WinForms application.

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 dataGridView_AnalogPoints = new dataGridView(); 

Create a new instance of a class object "AnalogInput" with public string properties initialized as shown below

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"

DataTable analogPoints = new DataTable();

Add the columns to the 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

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

Set the source of the 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? 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. I'm also not sure what I should be searching for specifically to solve this.

1
Either loop through the DataTable rows and create the “new” AnalogInput objects, OR, instead of creating a DataTable… make a List of AnalogInput objects (List<AnalogInput>) and use that as a DataSource to the grid. Without more information it is difficult to say how you should do this. Is the data in a data base?JohnG
I am retrieving each AnalogInput from a List<AnalogInput> that ultimately belongs to a class object Device. So retrieving this list would look something like myDevice.Inputs.Analog. The user selects a Device by selecting a TreeNode, then that is supposed to populate the DataGridView with the List<AnalogInput> from that Device.dc1562
If you already have a List<AnalogInput> then use that list as a data source to the grid. Example: List<AnalogInput> data = myDevice.Inputs.Analog; dataGridView_AnalogPoints.DataSource = data;. I am guessing there is more to this than you are showing.JohnG
If I set the data source that way how do I populate the DataGridView with rows containing the properties that I want to allow to be displayed and edited from the DataGridView belonging to each AnalogInput from myDevice.Inputs.Analog? From the example I want the properties of each AnalogInput: public string Name { get; set;},public string Address { get; set;},public string Minimum { get; set;}, and public string Maximum { get; set;} to be displayed in the DataGridView columns I defined in the example.dc1562
I am not following what you are asking. If you have a List<AnalogInput> which “contains” multiple AnalogInput objects in the list, then, setting this list as a data source to the grid should display such that the first column will be “Name”, the second column will be “Address” etc.… then “each” row in the grid will represent “one” AnalogInput object in the list.JohnG

1 Answers

0
votes

I don't think you are very far from achieving your intended behaviour. The DataGridView class has a wonderful event called 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. What you wish to do is run a function whenever a value of a cell was changed. 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".

The CellValueChanged event "wants" its subscribed methods to have the following parameters (their names don't matter, what matters is their order and type):

object sender, DataGridViewCellEventArgs e

The parameter sender is the object caused the method to be invoked (i.e your DataGridView) and e is of type DataGridViewCellEventArgs, which has some properties that'll be very helpful for you: RowIndex, and 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.