Click here to Skip to main content
15,799,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a class object and the Datagridview. I want to convert data in Datagridview to object, and can get/set values in object. Assume the quantity element in Datagridview equal to field of object, and variable name like the name first of Datagridview. I don't know whether we have the way to convert or not?
e.g., data of Datagridview with two columns:

ColumnX ColumnY
Name Join
Age 18
Location LA

And e.g. of class Person, the variable and quantity same of Datagridview:

Person _person = null;

private class Person
	private string Name {get;set;}
	private int Age {get;set;}
	private string Location {get;set}

private void GridviewToObject()
 				Person _person = new Person();
                for (int i = 0; i < dgv_gridview.Rows.Count; i++)
                    if (dgv_gridview.Rows[i].Cells[0].Value.ToString() == "Name")
                        _person.Name = 
                    if (dgv_gridview.Rows[i].Cells[0].Value.ToString() == "Age")
                        _person.Age = 
                    if (dgv_gridview.Rows[i].Cells[0].Value.ToString() == "Location")
                        _person.Location = 

private void bt_test_Click(object sender, EventArgs e)
           var _nameperson = _person.Name; //Join
           var _ageperson = _person.Age; //18
           var _locationperson = _person.Location //LA

What I have tried:

I have been finding some ideas of this case, any way of best.
Updated 19-Aug-23 11:06am
Ralf Meier 19-Aug-23 11:13am    
I suggest you should declare the _person outside the method GridviewToObject. In your sample this object only exists inside the method ...
Gerry Schmitz 19-Aug-23 11:19am    
Working with "grids" and no data source is like taking notes without a notebook.
Dave Kreskowiak 19-Aug-23 11:25am    
Why are you storing the data in a DGV and not binding the grid to an appropriate backing object, like a DataTable setup to hold the data?

By trying to use the DGV to store the data, you're making it much more difficult to write code to manipulate the data.
headshot9x 19-Aug-23 11:32am    
Anyway, DataGridview, Listview, DataTable or other winform control, the data showing in that and I want to transfer of those to object define, which used through program.
Richard MacCutchan 19-Aug-23 11:54am    
As others have suggested you need the data first. So create your list of Person objects and use that data to populate the DGV, not the other way round.

1 solution

Bear in mind that your processing is in a loop: so if your DGV has multiple persons, only the the last will be used - the earlier data will be discarded.

You can't access _person in your button click handler: it is local to the GridviewToObject and will be discarded when the method ends - the class itself will remain in existence on the heap, but you will have no way to access it.
I;'d suggest that you return a Person instance from the method, and save it in your class instance variables when you call it. That variable can then be accessed in your Click handler method.

BTW: You probably should change the Age field to a DateOfBirth instead since age can be out of date a second after it is entered...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900