usr021986
usr021986

Reputation: 3511

Bind Linq Result to datagridview

i have a linq result as var & query as following

     var groups = myDataTable.AsEnumerable()
              .GroupBy(r => r.Field<string>("X"))
              .Select(g => new { Name = g.Key,Count=g.Count() });

I want to bind the result to datagridview.

Please suggest

Thanks

Upvotes: 5

Views: 27830

Answers (3)

danyolgiax
danyolgiax

Reputation: 13086

updated

Have you tried this way?

yourGridView.DataSource=groups.ToList();
yourGridView.DataBind();

for WinForm apps only do this:

yourGridView.DataSource=groups.ToList();

Upvotes: 5

Navaneethan
Navaneethan

Reputation: 2215

Try this

    var groups = (myDataTable.AsEnumerable()
                  .GroupBy(r => r.Field<string>("X"))
                  .Select(g => new { Name = g.Key,Count=g.Count() })).ToList();
gridview1.DataSource=groups;

Upvotes: 1

Akram Shahda
Akram Shahda

Reputation: 14771

Try the following:

dataGridView.DataSource = groups.ToList();

Upvotes: 14

Related Questions