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.
DataTable
rows and create the “new”AnalogInput
objects, OR, instead of creating aDataTable
… make aList
ofAnalogInput
objects (List<AnalogInput>
) and use that as aDataSource
to the grid. Without more information it is difficult to say how you should do this. Is the data in a data base? – JohnGAnalogInput
from aList<AnalogInput>
that ultimately belongs to a class objectDevice
. So retrieving this list would look something likemyDevice.Inputs.Analog
. The user selects aDevice
by selecting aTreeNode
, then that is supposed to populate theDataGridView
with theList<AnalogInput>
from thatDevice
. – dc1562List<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. – JohnGDataGridView
with rows containing the properties that I want to allow to be displayed and edited from theDataGridView
belonging to eachAnalogInput
frommyDevice.Inputs.Analog
? From the example I want the properties of eachAnalogInput
:public string Name { get; set;}
,public string Address { get; set;}
,public string Minimum { get; set;}
, andpublic string Maximum { get; set;}
to be displayed in theDataGridView
columns I defined in the example. – dc1562List<AnalogInput>
which “contains” multipleAnalogInput
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