Hi priyajsr,
You are assigning List to GvOrder gridview. So you can't directly convert List to DataTable as the GridView DataSource is List. So you need to first convert the List to DataTable and then bind to gridview. Form that gridview you can get the DataSource as DataTable by type casting it.
Refer the below sample.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Download and install Northwind Database
HTML
<table>
<tr>
<td>
<asp:GridView runat="server" ID="GvOrder" Caption="From Entity" />
</td>
<td>
<asp:GridView runat="server" ID="GvOrder1" Caption="From DataSource" />
</td>
</tr>
</table>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
NorthwindEntities entity = new NorthwindEntities();
var db1 = (from ordermst in entity.Orders
where ordermst.ShipCountry.Equals("Finland")
select new
{
CustomerID = ordermst.CustomerID,
OrderID = ordermst.OrderID
}).Take(10).ToList();
DataTable dt = ConvertToDataTable(db1);
GvOrder.DataSource = dt;
GvOrder.DataBind();
DataTable dt1 = new DataTable();
dt1 = GvOrder.DataSource as DataTable;
GvOrder1.DataSource = dt1;
GvOrder1.DataBind();
}
}
private DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
{
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
}
table.Rows.Add(row);
}
return table;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim entity As New NorthwindEntities()
Dim db1 = (From ordermst In entity.Orders Where (ordermst.ShipCountry.Equals("Finland")) _
Select New With { _
Key .CustomerID = ordermst.CustomerID, _
Key .OrderID = ordermst.OrderID _
}).Take(10).ToList()
Dim dt As DataTable = ConvertToDataTable(db1)
GvOrder.DataSource = dt
GvOrder.DataBind()
Dim dt1 As New DataTable()
dt1 = TryCast(GvOrder.DataSource, DataTable)
GvOrder1.DataSource = dt1
GvOrder1.DataBind()
End If
End Sub
Private Function ConvertToDataTable(Of T)(data As IList(Of T)) As DataTable
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
Dim table As New DataTable()
For Each prop As PropertyDescriptor In properties
table.Columns.Add(prop.Name, If(Nullable.GetUnderlyingType(prop.PropertyType), prop.PropertyType))
Next
For Each item As T In data
Dim row As DataRow = table.NewRow()
For Each prop As PropertyDescriptor In properties
row(prop.Name) = If(prop.GetValue(item), DBNull.Value)
Next
table.Rows.Add(row)
Next
Return table
End Function
OutPut
From Entity
CustomerID | OrderID |
WARTH |
10266 |
WARTH |
10270 |
WARTH |
10320 |
WARTH |
10333 |
WARTH |
10412 |
WARTH |
10416 |
WARTH |
10437 |
WARTH |
10455 |
WARTH |
10526 |
WARTH |
10553 |
|
From DataSource
CustomerID | OrderID |
WARTH |
10266 |
WARTH |
10270 |
WARTH |
10320 |
WARTH |
10333 |
WARTH |
10412 |
WARTH |
10416 |
WARTH |
10437 |
WARTH |
10455 |
WARTH |
10526 |
WARTH |
10553 |
|