Datagrid

G

Guest

Hello,
I have a Datareader with 2 fields from the database in it. I would like
to use the DataGrid to display them. This I can do. If the DataReader
has 4 rows in it I would like to display the records in 2 DataGrid
rows. So I would have the following:

ColHeader1 ColHeader2 ColHeader1 ColHeader2
Field1(0) Field2(0) Field1(1) Field2(1)
Field1(2) Field2(2) Field1(3) Field2(3)

Is this possible with the DataGrid? If so could you please point me in
the right direction? I have tried everything I can think of.

Thanks
 
A

addup

With a datagrid.... very difficult

It's easier with a repeater

Example (VB)

<ASP:REPEATER Runat=server DataSource="<%# DummyData %>">
<HEADERTEMPLATE><TABLE>
<TR><TH>Even</TH><TH>Even</TH><TH>Odd</TH><TH>Odd</TH></TR>
</HEADERTEMPLATE>
<ITEMTEMPLATE>
<TR><TD><%# DataBinder.Eval(Container.Dataitem, "Caption" ) %></TD>
<TD><%# DataBinder.Eval(Container.Dataitem, "Caption" ) %></TD>
<%# IIF( Container.ItemIndex = CTYPE(Container.Parent,
System.Web.UI.WebControls.Repeater).DataSource.Rows.Count - 1, "</TR>",
"") %>
</ITEMTEMPLATE>
<ALTERNATINGITEMTEMPLATE>
<TD><%# DataBinder.Eval(Container.Dataitem, "Caption" ) %></TD>
<TD><%# DataBinder.Eval(Container.Dataitem, "Caption" ) %></TD>
</TR>
</ALTERNATINGITEMTEMPLATE>
<FOOTERTEMPLATE>
<TR><TH>Even</TH><TH>Even</TH><TH>Odd</TH><TH>Odd</TH></TR>
</TABLE>
</FOOTERTEMPLATE>
</ASP:REPEATER>

Where my DummyData is a datatable with one "Caption" Column
Clever, eh?

NOTE: the IIF is to properly terminate the <TR> in case you have an odd
number of members in your datasource.

the original HTML specs state that <TR>s don't have to be terminated,
so you don't *have* to use it if you are not serving HTML 4 / XHTML

Hope this helps
-- addup --
 
A

addup

I didn't notice the datareader in your original post

a (slightly) better way of doing this would be
<ITEMTEMPLATE>
<TR><TD><%# DataBinder.Eval(Container.Dataitem, "Caption" ) %></TD>
<TD><%# DataBinder.Eval(Container.Dataitem, "Caption" ) %></TD>
</ITEMTEMPLATE>


and
<FOOTERTEMPLATE>
<%# IIF( CTYPE(Container.Parent,
System.Web.UI.WebControls.Repeater).Items.Count Mod 2, "</TR>", "") %>
<TR><TH>Even</TH><TH>Even</TH><TH>Odd</TH><TH>Odd</TH></TR>
</TABLE>
</FOOTERTEMPLATE>
 
G

Guest

Thank you for your quick reply. Looks like it is going to work. I will
try it and get back to you. Thanks again and happy holidays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top