Home All Groups Group Topic Archive Search About
Author
28 Dec 2005 4:32 PM
unwantedspam
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

Author
28 Dec 2005 6:02 PM
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 --
Author
28 Dec 2005 6:22 PM
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>
Author
28 Dec 2005 6:28 PM
unwantedspam
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.