I have some List for storage data
List<List<string>> data = new List<List<string>>();
How to correctly assign it to dataGridView.DataSource ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem here is (obviously) the DataGridView DataSource property doesn’t know how to display a
List<List<string>>. One approach is to combine your Lists into one object which the DataGridView can bind to. Here are a couple ways to do this:To a DataTable:
To convert your
List<List<string>>to a DataTable, I borrowed the code found here and created this extension method:Here’s now you can use this extension method to get a DataTable from your
List<List<string>>which you can bind to your DataGridView:To an anonymous type List:
Here is the code for doing this:
We need the anonymous type because a DataGridView won’t be able to display the string values in List property without a little help, as described here.
There are of course disadvantages to either these approaches but I do believe something along these lines is your best option.