CoolArchTek
CoolArchTek

Reputation: 3839

Asp.net bind datatable to gridview

I know how to bind a simple datatable to a gridview, but this is a different scenario (I think). I am calling a class library dll which returns a class. I can say its kind of list.

I will call it like,

Dim demo = New ABCDataTable()
demo = demo.GetTheDataTable(MyConnectionString)
GridView1.DataSource = demo
GridView1.DataBind()

Question: How do I bind this to gridview in a markup file? Which datasource control I have to use?

Update:

I used ObjectDataSource and assigned SelectMethod="GetTheDataTable" and used selectparameter to pass connection string.

I am assigning connection string in the code behind ObjectDataSource1.SelectParameters["connectionString"].DefaultValue = MyConnectionString;

but I am getting an error.

Upvotes: 0

Views: 2450

Answers (2)

th1rdey3
th1rdey3

Reputation: 4358

i don't know if it will work but try like this -

<asp:GridView id="GridView1" runat="server" 
   DataSource='<%# (new ABCDataTable()).GetTheDataTable(ConfigurationManager.ConnectionString["nameofyourconnectionstringInwebconfigfile"])) %>' >

</asp:GridView>

Upvotes: 1

Manoj De Mel
Manoj De Mel

Reputation: 995

You can use a Hidden control as a select parameter for connectionString.

Make sure the default constructor of the class ABCDataTable does not have any parameters.

If it does, then you could create a static method in another class to make the instance and return the result to ObjectDatasource.

Upvotes: 0

Related Questions