Accessing Collection works

  • Thread starter Thread starter James Zhuo
  • Start date Start date
J

James Zhuo

Accessing Items in a Collection works with the [] as suggested by Kevin and
Chris (thx)
But when i tried to use the same technic with databinding within the aspx
file, it doesn't seem to work.
This is the aspx part that i am talking about

<asp:DataGrid id="dgThreads" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Databases">
<ItemTemplate>
<%# Container.DataItem["author_id"] %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Compiler Error Message: CS0021: Cannot apply indexing with [] to an
expression of type 'object'

I've tried to look up what functions and properties that are available with
DataItem with no success.

Alternatively i could have changed the TemplateColumn to a BoundColumn and
simply specify the datafield I want, this approach works, but this lacks
flexibility when i want to create customized columns. So What i want to do
is know how I can bind data explicitly to a TemplateColumn explicitly using
<%# ......................%>

Please help again.


Cheers

j
 
I found a solution to my own problem, simply use

<%# DataBinder.Eval(Container.DataItem, "DATABASE_NAME") %>
in place of
<%# Container.DataItem["author_id"] %>

Cheer and Thanx again for the help

J

James Zhuo said:
Accessing Items in a Collection works with the [] as suggested by Kevin and
Chris (thx)
But when i tried to use the same technic with databinding within the aspx
file, it doesn't seem to work.
This is the aspx part that i am talking about

<asp:DataGrid id="dgThreads" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Databases">
<ItemTemplate>
<%# Container.DataItem["author_id"] %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Compiler Error Message: CS0021: Cannot apply indexing with [] to an
expression of type 'object'

I've tried to look up what functions and properties that are available with
DataItem with no success.

Alternatively i could have changed the TemplateColumn to a BoundColumn and
simply specify the datafield I want, this approach works, but this lacks
flexibility when i want to create customized columns. So What i want to do
is know how I can bind data explicitly to a TemplateColumn explicitly using
<%# ......................%>

Please help again.


Cheers

j



Hi all

I have been developing some web apps using VB.NET for a coupla month.

Until recently I have tried to use C# instead of VB. What i find is that in
VB.NET, Collections can be accessed using the dictionary approach for
example ds.Tables("Threads") as below

Dim DBConnection As New OleDbConnection = "blah .. .. . .. "
Dim DBCommand As New OleDbCommand(DBQueryString, DBConnection)
Dim DBAdapter As New OleDbDataAdapter(DBCommand)
Dim DSThreads As New DataSet()
DBConnection.Open()
DBAdapter.Fill(DSThreads, "Threads")
DBConnection.Close()
--> dgThreads.DataSource = ds.Tables("Threads")
dgThreads.DataBind()

What I want to know is how I would do the same thing ds.Tables("Threads")
using C#. I want to retrieve the table "Threads" that is store in the
dataset. ds.Tables is a a property that returns a DataTableColletion. There
seem to be no straight forward way to access the DataTable that is contained
in the collection.

Please help, any suggestion is much appreciated.

Cheer

J
 
Even though i found a solution for the problem
I would still like to know how i might be able to do the equivalent of
<%# Container.DataItem("author_id")%>
using C#

Cheers

J



James Zhuo said:
Accessing Items in a Collection works with the [] as suggested by Kevin and
Chris (thx)
But when i tried to use the same technic with databinding within the aspx
file, it doesn't seem to work.
This is the aspx part that i am talking about

<asp:DataGrid id="dgThreads" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Databases">
<ItemTemplate>
<%# Container.DataItem["author_id"] %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Compiler Error Message: CS0021: Cannot apply indexing with [] to an
expression of type 'object'

I've tried to look up what functions and properties that are available with
DataItem with no success.

Alternatively i could have changed the TemplateColumn to a BoundColumn and
simply specify the datafield I want, this approach works, but this lacks
flexibility when i want to create customized columns. So What i want to do
is know how I can bind data explicitly to a TemplateColumn explicitly using
<%# ......................%>

Please help again.


Cheers

j



Hi all

I have been developing some web apps using VB.NET for a coupla month.

Until recently I have tried to use C# instead of VB. What i find is that in
VB.NET, Collections can be accessed using the dictionary approach for
example ds.Tables("Threads") as below

Dim DBConnection As New OleDbConnection = "blah .. .. . .. "
Dim DBCommand As New OleDbCommand(DBQueryString, DBConnection)
Dim DBAdapter As New OleDbDataAdapter(DBCommand)
Dim DSThreads As New DataSet()
DBConnection.Open()
DBAdapter.Fill(DSThreads, "Threads")
DBConnection.Close()
--> dgThreads.DataSource = ds.Tables("Threads")
dgThreads.DataBind()

What I want to know is how I would do the same thing ds.Tables("Threads")
using C#. I want to retrieve the table "Threads" that is store in the
dataset. ds.Tables is a a property that returns a DataTableColletion. There
seem to be no straight forward way to access the DataTable that is contained
in the collection.

Please help, any suggestion is much appreciated.

Cheer

J
 
Back
Top