C# Using Linq to Query DataTable and Bind to Gridview

Last post 06-12-2008 6:38 AM by dotnetukguru. 25 replies.

Sort Posts:

  • C# Using Linq to Query DataTable and Bind to Gridview

    08-24-2007, 8:10 PM
    • Loading...
    • Bridge23
    • Joined on 08-25-2007, 12:01 AM
    • Posts 6

    I have been trying to figure out, to no avail, how to query a datatable with linq and than bind those results to a grid view.  How do you select specific columns from the table and then bind to a gridview?  I am sure others are trying to figure out the same thing as this would be very useful to be able to do.  Here is a copy of the syntax I have been trying:

    var query = from c in table.AsIEnumerable() select c;

    gridview1.datasource = query;

    gridview1.databind();

    Do you select specific columns by c.Field<string>("username") or c.username

    I am using Visual Studio 8 Beta2.  Any help would be greatly appreciated.

    Cheers!

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    08-25-2007, 8:26 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 7:10 AM
    • Croatia
    • Posts 1,356

    Try with this:

            NorthWindDataContext db = new NorthWindDataContext();
            var products = from p in db.Products select new { p.CategoryID, p.Discontinued };
            GridView1.DataSource = products;
            GridView1.DataBind();

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    08-28-2007, 10:59 AM
    • Loading...
    • Bridge23
    • Joined on 08-25-2007, 12:01 AM
    • Posts 6

    kipo:

    Try with this:

            NorthWindDataContext db = new NorthWindDataContext();
            var products = from p in db.Products select new { p.CategoryID, p.Discontinued };
            GridView1.DataSource = products;
            GridView1.DataBind();

    Error 1 'System.Data.DataRow' does not contain a definition for 'Person' and no extension method 'Person' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?) C:\Users\MyAccount\Documents\Visual Studio 2008\WebSites\GWLCDev\Default.aspx.cs 41 66 C:\...\Dev\
     

    This is the error I get when I try and use code similar to what your response.  Any Ideas?

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    08-28-2007, 1:11 PM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 7:10 AM
    • Croatia
    • Posts 1,356

    Can you post code which is generating error?

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    08-28-2007, 1:33 PM
    • Loading...
    • Bridge23
    • Joined on 08-25-2007, 12:01 AM
    • Posts 6
    DataTable table = (DataTable)Session["Save"];

    var query = from c in table.AsEnumerable()

    select new { c.Week,c.Person,c.Guess,c.Actual};

    GridView2.DataSource = query;

    GridView2.DataBind();

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    08-29-2007, 3:23 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 7:10 AM
    • Croatia
    • Posts 1,356

    Sorry, I thought you're using Linq to SQL, but actually you're using Linq to DataTable. So in your case use this:

    var query = from c in table.AsEnumerable() select new { Week = c.Field<string>("Week"), Person = c.Field<string>("Person"), Guess = c.Field<string>("Guess"), Actual = c.Field<string>("Actual") };

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    08-29-2007, 9:01 AM
    • Loading...
    • Bridge23
    • Joined on 08-25-2007, 12:01 AM
    • Posts 6

    Kipo,

     Yes, that worked thank you for taking the time to help me out. Kudos to you!  select new { c.Week,c.Person,c.Guess,c.Actual} only works if you are using a typed dataset, I did not realize that.

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    10-29-2007, 5:21 PM
    • Loading...
    • Vir1
    • Joined on 10-29-2007, 9:18 PM
    • Posts 1

    Thanks ....

    I am working on same problem but i have to select perticular rows depanding on select criteria

    can you please explain how can we do that

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    10-30-2007, 2:51 PM
    • Loading...
    • Bridge23
    • Joined on 08-25-2007, 12:01 AM
    • Posts 6

    Vir1:

    Thanks ....

    I am working on same problem but i have to select perticular rows depanding on select criteria

    can you please explain how can we do that

    Just use the Where keywork.

    var query = from c in table.AsEnumerable() WHERE c.Field<string>("Week") = 25  select new { Week = c.Field<string>("Week"), Person = c.Field<string>("Person"), Guess = c.Field<string>("Guess"), Actual = c.Field<string>("Actual") };
     
    My syntax may be a little off but you get the basic idea.
  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    04-28-2008, 8:35 AM

    I am little bit late..but would like to share my example with you,

      DataTable dt = new DataTable();

    dt.Columns.Add("col1");

    dt.Columns.Add("col2");

    dt.Columns.Add("col3");

    int i = 0;while (i < 100)

    {

    DataRow dr = dt.NewRow();

    dr["col1"] = i;

    dr["col2"] = i + " - Name";

    dr["col3"] = i + " - Address";

    dt.Rows.Add(dr);

    i++;

    }

    dt.AcceptChanges();

    var result = from p in dt.AsEnumerable()select new {col1=p.Field<string>("col1"),col2=p.Field<string>("col2"),col3=p.Field<string>("col3")};

    GridView1.DataSource = result;

    GridView1.DataBind();

  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    05-11-2008, 1:06 PM

    Hi i am wondering if you coud please help me too i am having the same type of problem as Bridge23,

     I am trying to do a Linq to a Cached DataTable, i am quite new to Linq...

    I emulated his code from the solution  you gave to Bridge23, the only thing i am doing differently is that i am storing my dataset in a cache instead of a session...can this be the problem i really thing not ...here is my code

     

    and below it is the error i am receiving :

    DataSet ds = (DataSet) Cache["dsResult"];

    DataTable dt = ds.Tables[0]; var query = from c in dt.AsEnumerable()

    select new

    {

    SecurityCode = c.Field<
    string>("SecurityCode"),

    SecurityName = c.Field<string>("SecurityName"),

    Currency = c.Field<string>("Currency"),

    Product = c.Field<string>("Product"),

    CountryIssue = c.Field<string>("CountryIssue"),

    Location = c.Field<string>("Location"),

    Tag = c.Field<string>("Tag"),

    AvgBorrowCost = c.Field<string>("AvgLoanCost"),

    AvgLoanCost = c.Field<string>("AvgLoanCost"),

    AvgBorrowRebate = c.Field<string>("AvgBorrowRebate"),

    AvgLoanRebate = c.Field<string>("AvgLoanRebate"),

    Price = c.Field<string>("Price"),

    SettledTotal = c.Field<string>("SettledTotal"), SettledValue = c.Field<string>("SettledValue"),

    //PendingDate1 = c.Field<string>("PendingDate1"),

    //PendingValue1 = c.Field<string>("PendingValue1"),

    //NetDate1 = c.Field<string>("NetDate1"),

    //NetValue1 = c.Field<string>("NetValue1"),

     

    //PendingDate2 = c.Field<string>("PendingDate2"),

    //PendingValue2 = c.Field<string>("PendingValue2"),

    //NetDate2 = c.Field<string>("NetDate2"),

    //NetValue2 = c.Field<string>("NetValue2"),

    //PendingDate3 = c.Field<string>("PendingDate3"),

    //PendingValue3 = c.Field<string>("PendingValue3"),

    //NetDate3 = c.Field<string>("NetDate1"),

    //NetValue3 = c.Field<string>("NetValue1"),

    ////pending and net 4 to 22 here

    Total = c.Field<string>("Total"),

     

    };

    gvPositionManager.DataSource = query;

    gvPositionManager.DataBind();  //Error occurs at this point! but i though a Datable does support Sever-side paging??

     

    }

    Error i get is:

    The data source does not support server-side data paging

    This above all: to thine own self be true.

    William shakespeare, Hamlet
  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    05-11-2008, 4:29 PM
    • Loading...
    • azamsharp
    • Joined on 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,248

    Are you also using LINQ to SQL or only LINQ?

    Hope it helps!
    AzamSharp
    Blog AzamSharp
    RefactorCode
    GridViewGuy
  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    05-11-2008, 5:36 PM

    Hi

    I am using just Linq

    This above all: to thine own self be true.

    William shakespeare, Hamlet
  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    05-11-2008, 5:39 PM

    If you are asking if i am using the Linq to SQL class file and then creating an instance of the DBContext file i am not, but if there is a way to do it that way i am intersted as well

    thanks

    This above all: to thine own self be true.

    William shakespeare, Hamlet
  • Re: C# Using Linq to Query DataTable and Bind to Gridview

    05-11-2008, 7:00 PM
    • Loading...
    • azamsharp
    • Joined on 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,248

     Well if you are using VS 2008 then you can simply add a "Linq to Sql" file and map it to your database.

    Hope it helps!
    AzamSharp
    Blog AzamSharp
    RefactorCode
    GridViewGuy
Page 1 of 2 (26 items) 1 2 Next >