This is a migrated thread and some comments may be shown as answers.

Gridview with Linq to sql

1 Answer 25 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tiaan
Top achievements
Rank 1
Tiaan asked on 14 Mar 2014, 12:48 PM
Good day

I have a gridview with predefined columns . I have a linq to sql query where i name my selected values with the same name as the predefined column names.
After i set the datasource it adds new columns to the end of the gridview.
Here is my sql query


            var res = (from s in db.SiteAudits
                       where s.SiteAuditID > 0000250 && s.SiteAuditID < 0000300
                       join ac in db.Accounts on s.AccountID equals ac.AccountID
                       join si in db.Sites on s.SiteID equals si.SiteID
                       join au in db.Audits on s.AuditID equals au.AuditID
                       select new
                       {
                         _siteauditid = s.SiteAuditID,
                         _account = ac.AccountName + "-" + si.SiteName,
                         _datedue = s.DateDue,
                         _audit = au.AuditTitle
                         
                       }).ToList();


            grd_unscheduledaudits.DataSource = res;

Is there a special way my columns should link to my query's result columns ?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Mar 2014, 01:29 PM
Hello Tiaan,

Thank you for contacting Telerik Support.

In order to link the manually added columns with the ones, coming from the DataSource, it is necessary to specify the FieldName property of the corresponding column:
private void Form1_Load(object sender, EventArgs e)
{
    this.order_DetailsTableAdapter.Fill(this.nwindDataSet.Order_Details);
 
    GridViewDecimalColumn orderIdColumn = new GridViewDecimalColumn();
    orderIdColumn.FieldName = "OrderID";
    radGridView1.MasterTemplate.Columns.Add(orderIdColumn);
 
    GridViewDecimalColumn productIdColumn = new GridViewDecimalColumn();
    productIdColumn.FieldName = "ProductID";
    radGridView1.MasterTemplate.Columns.Add(productIdColumn);
 
    var res = (from od in this.nwindDataSet.Order_Details
               where od.ProductID > 10 && od.Quantity > 3
               select new
               {
                   od.OrderID,
                   od.ProductID
               }).ToList();
 
    this.radGridView1.DataSource = res;
}

Our Generating Columns help article is quite useful about this topic.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
Tags
GridView
Asked by
Tiaan
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or