LINQ to dataview........
-
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
-
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
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