Datagrid

Discussion in 'ASP .Net Web Controls' started by unwantedspam, Dec 28, 2005.

  1. unwantedspam

    unwantedspam 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
    unwantedspam, Dec 28, 2005
    #1
    1. Advertising

  2. unwantedspam

    addup Guest

    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 --
    addup, Dec 28, 2005
    #2
    1. Advertising

  3. unwantedspam

    addup Guest

    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>
    addup, Dec 28, 2005
    #3
  4. unwantedspam

    unwantedspam 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.
    unwantedspam, Dec 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    0
    Views:
    425
  2. BK Kim

    datagrid in datagrid

    BK Kim, Feb 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    518
    Alvin Bruney [MVP]
    Mar 2, 2004
  3. =?Utf-8?B?U3VyZXNo?=
    Replies:
    0
    Views:
    503
    =?Utf-8?B?U3VyZXNo?=
    Mar 2, 2004
  4. Brian K. Williams
    Replies:
    0
    Views:
    530
    Brian K. Williams
    Mar 2, 2004
  5. Andreas Klemt
    Replies:
    0
    Views:
    258
    Andreas Klemt
    Oct 8, 2003
Loading...

Share This Page