I'm having a Structure like
X={ID="1", Name="XX",
ID="2", Name="YY" };
How to dump this data to a DataGridView
of two columns
The gridView is like
ID | Name
Can we use LINQ to do this. I'm new to DataGridView
Pleaese help me to do this..
Thanks in advance
you shoud do like this form your code
DataGridView.DataSource = yourlist;
DataGridView.DataBind();
Let's assume you have a class like this:
And assume you have dragged and dropped a
DataGridView
to your form, and name it dataGridView1.You need a BindingSource to hold your data to bind your
DataGridView
. This is how you can do it:first you need to add 2 columns to datagrid. you may do it at design time. see Columns property. then add rows as much as you need.
LINQ is a "query" language (thats the Q), so modifying data is outside its scope.
That said, your
DataGridView
is presumably bound to anItemsSource
, perhaps of typeObservableCollection<T>
or similar. In that case, just do something likeX.ToList().ForEach(yourGridSource.Add)
(this might have to be adapted based on the type of source in your grid).My favorite way to do this is with an extension function called 'Map':
Then you can add all the rows like so: