4

I failed to find a sample about how to convert an array instances of user defined types into a (C# ADO.Net) datatable -- I want to use the datatable to bind to ASP.Net data bound controls (e.g. gridview). Could anyone provide a sample or recommend me to some simple samples?

Another question is, whether it is a must to convert to datatable in order to bound to controls in ASP.Net? Could I bound to any array of user defined types?

thanks in advance, George

1
  • if you are doing a GridView1.DataSource = Array & that did not display anything please note you also need to do GridView1.DataBind() after that. Commented Feb 1, 2012 at 20:04

4 Answers 4

3

No, you don't need a DataTable to do binding - in fact, most binding now is object based, especially with LINQ (which generates an object model, not a DataTable). Just use the public property names (from your class) as the data-members for different columns, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Marc, glad to see you here. :-) Sorry I can not use LINQ for non-technical reasons. Are there any other solutions for me if not using DataTable/DataSet?
3

I had the same requirement and I found a nice article at Pauls Blog. You can have a look at the following link if you still wanted it

http://weblogs.asp.net/psperanza/archive/2005/05/18/407389.aspx

Here is the code that I have used in my project. Hope that helps

Public Function ConvertArrayToDatatable(ByVal arrList As ArrayList) As DataTable
    Dim dt As New DataTable
    Try
        If arrList.Count > 0 Then
            Dim arrype As Type = arrList(0).GetType()
            dt = New DataTable(arrype.Name)

            For Each propInfo As PropertyInfo In arrype.GetProperties()
                dt.Columns.Add(New DataColumn(propInfo.Name))
            Next

            For Each obj As Object In arrList
                Dim dr As DataRow = dt.NewRow()

                For Each dc As DataColumn In dt.Columns
                    dr(dc.ColumnName) = obj.GetType().GetProperty(dc.ColumnName).GetValue(obj, Nothing)
                Next

                dt.Rows.Add(dr)
            Next
        End If

        Return dt
    Catch ex As Exception
        Return dt
    End Try

End Function

Comments

1

I would suggest using the ObjectDataSource class to do what you want. Then you won't need to use a DataTable at all, and you'll still be able to get data binding. ObjectDataSource allows you to expose user defined types to data binding, and it doesn't matter how the types are obtained; they don't need to come from a database at all, if that's what you need.

2 Comments

I read the link you recommended. Currently, my application is 3 tiers, ASP.Net -- Web Service -- Database. In the sample of the link, ASP.Net queries data source directly which violates my design purpose to make Web Service layer in the middle to transparent data access. Comments or better samples?
Is the example you're referring to the one that references EmployeeLogic? If so, change the implementation of the GetAllEmployees() method to call the Web Service instead using a SqlDataSource. ObjectDataSource works with any kind of object, so you can obtain the data from anywhere you need to.
0

In regards to the comment "can not use LINQ for non-technical reasons":

I presume you don't want to use LINQ to SQL since you want to maintain your three-tiered architecture. But you may not know that you can use LINQ to Objects and that would provide the flexibility you seek.

Suppose you have a middle tier method: List Tier2.SomeMethod(). You can assign that return value of that method as the GridView data source and then specify SomeObject methods/properties in the GridView control: gvSomeGridView.DatasSource = Tier2.SomeMethod(); gvSomeGridView.DataBind();

Further you can then retrieve SomeObject from the DataItem in GridView callbacks.

HTH.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.