Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. LINQ to dataview........

LINQ to dataview........

Scheduled Pinned Locked Moved ASP.NET
csharpdatabaselinqalgorithms
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pranavcool
    wrote on last edited by
    #1

    Hi, I have written a linq query and bind it to a gridview. Now i want to implement sorting on gridview columns for which i want this linq query to store in dataview. So how it is possible to store linq query in dataview. the following is the code which i have written to bind gridview.

        protected void BindGrid()
        {
            try
            {
                var employees = from e in DetailContext.Employees orderby 
                e.Emp\_Code descending 
                select new {e.Emp\_Id,e.Emp\_Code,e.F\_Name,e.L\_Name,e.Designation };
                GridView1.DataSource = employees;                
                GridView1.DataBind(); 
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }  
    

    Pranav Dave

    P 1 Reply Last reply
    0
    • P pranavcool

      Hi, I have written a linq query and bind it to a gridview. Now i want to implement sorting on gridview columns for which i want this linq query to store in dataview. So how it is possible to store linq query in dataview. the following is the code which i have written to bind gridview.

          protected void BindGrid()
          {
              try
              {
                  var employees = from e in DetailContext.Employees orderby 
                  e.Emp\_Code descending 
                  select new {e.Emp\_Id,e.Emp\_Code,e.F\_Name,e.L\_Name,e.Designation };
                  GridView1.DataSource = employees;                
                  GridView1.DataBind(); 
              }
              catch (Exception ex)
              {
                  throw ex;
              }
          }  
      

      Pranav Dave

      P Offline
      P Offline
      padmanabhan N
      wrote on last edited by
      #2

      There is one .cs for converting linq to datatable //.cs file using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Data; public class LinqToDataTable { public static DataTable ObtainDataTableFromIEnumerable(System.Collections.IEnumerable ien) { DataTable dt = new DataTable(); try { foreach (object obj in ien) { Type t = obj.GetType(); PropertyInfo[] pis = t.GetProperties(); if (dt.Columns.Count == 0) { foreach (PropertyInfo pi in pis) { if (pi.PropertyType.Name == "Nullable`1") { string str = pi.PropertyType.FullName; str = str.Replace("System.Nullable`1", ""); str = str.Substring(str.IndexOf("System")); str = str.Substring(0, str.IndexOf(",")); dt.Columns.Add(pi.Name, str.GetType()); } else dt.Columns.Add(pi.Name, pi.PropertyType); } } DataRow dr = dt.NewRow(); foreach (PropertyInfo pi in pis) { object value = pi.GetValue(obj, null); dr[pi.Name] = value; } dt.Rows.Add(dr); } } catch (Exception) { dt = null; } return dt; } } //DataTable dtEmployees = LinqToDataTable.ObtainDataTableFromIEnumerable((IEnumerable)employees); now assign the datatable in dataview

      Padmanabhan

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups