Presenting records over multiple rows?

Discussion in 'ASP .Net Datagrid Control' started by Per Salmi, Feb 9, 2004.

  1. Per Salmi

    Per Salmi Guest

    I wonder if it is easy to use the DataGrid to display for example 4 data
    values related to a record on one grid-row and then have a 5th grid-row
    spanning all 4 columns that displays the 5th data value?

    This would then repeat for each record in the datatable showing each record
    on 2 rows. The first row with a few short data values and the second row
    showing a long text string.

    Or would it be better to build this with a repeater instead of the DataGrid?

    /Per
     
    Per Salmi, Feb 9, 2004
    #1
    1. Advertisements

  2. Hi Per,

    Thank you for posting in the community!

    Based on my understanding, you want to show your each record in 2 rows:
    first row contains 4 fields, while the second row contains the 5th field
    record, which spans the whole row's length.

    ================================================
    The datagrid control ¨¬s a highly tailored control, which will render one
    record in one row.
    Based on my experience, Repeater control gives your more control on
    databinding render. You may use Repeater control to render one record as 2
    rows.

    I have used Sql Server's default "jobs" table in "pubs" database to write a
    sample project for you:

    Html view code:
    <asp:repeater id="Repeater1" runat="server">
    <HeaderTemplate>
    <table align="center" border="1">
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td runat="server" id="job_id" align="center"><%#
    DataBinder.Eval(Container.DataItem,"job_id")%></td>
    <td runat="server" id="min_lvl" align="center"><%#
    DataBinder.Eval(Container.DataItem,"min_lvl")%></td>
    <td runat="server" id="max_lvl" align="center"><%#
    DataBinder.Eval(Container.DataItem,"max_lvl")%></td>
    </tr>
    <tr>
    <td runat="server" id="job_desc" align="center" colspan="3"><%#
    DataBinder.Eval(Container.DataItem,"job_desc")%></td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:repeater>

    Code behind:
    private void Page_Load(object sender, System.EventArgs e)
    {
    SqlDataAdapter adapter=new SqlDataAdapter("select * from
    jobs","server=localhost;database=pubs;uid=sa;pwd=");
    DataSet ds=new DataSet();
    adapter.Fill(ds);
    Repeater1.DataSource=ds.Tables[0];
    Repeater1.DataBind();
    }

    In the code, for each <ItemTemplate>, I use 2 rows, first with 3 <td>s,
    while second with 1 <td>. I add the attribute colspan=3, which makes this
    <td> span 3 <td> length(The whole <tr>)

    =================================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.
    Have a nice day!!

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 10, 2004
    #2
    1. Advertisements

  3. Per Salmi

    Per Salmi Guest

    Thanks for the solution, I tested it and it was exactly the thing I was
    looking for.

    I also managed to do the same thing with a DataGrid. That was possible by
    implementing a handler for the ItemDataBound event where I create a
    DataGridItem with a TableCell using the ColSpan property, setting the Text
    property of the TableCell and finally adding the DataGridItem to the
    Controls collection of the DataGrid.

    Best regards,
    Per Salmi




     
    Per Salmi, Feb 10, 2004
    #3
  4. Hi Per,

    Thanks very much for your feedback.

    I am glad my reply makes sense to you.

    Yes, you also can hook into the ItemDataBound event and add the TableRow
    dynamicly. But I think use the Repeater control is a more freely way to
    handle this issue.

    Anyway, it works :)

    If you have any further concern, please feel free to post, I will help you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 11, 2004
    #4
  5. Per Salmi

    Per Salmi Guest

    Ok, now I am back using the Repeater because I need to add checkboxes on a
    column for some of the items that I display.

    I got this working by adding asp:checkbox tags to the ItemTemplate inside TD
    tags.
    I also added paging to the repeater with PagedDataSource and now I need to
    get the checked rows when the user moves to a new page or clicks a button.

    I tried to get the Checked property from the Checkbox controls by using
    Repeater1.Items and FindControl("cbMyCheckBox") but the Checked property is
    always false no matter if the checkbox was checked or not!

    Is it not possible to find out which RepeaterItems were checked by looping
    over the Items in the Repeater? Why are they all "unchecked"?

    Best regards,
    Per Salmi
     
    Per Salmi, Feb 12, 2004
    #5
  6. The key to this may be that ViewState is turned off. Then they will all be
    default unchecked.

    Ben Miller
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.


     
    Ben Miller [MSFT]\\, Feb 12, 2004
    #6
  7. Hi Per,

    Thanks for posting your further issue.

    Based on my understanding, you still use the Repeater control to display
    the data, and you add an extra checkbox column for each Repeater Item(In
    your situation, every 2 rows), but when you check the CheckBox stats at
    server side, you find that they are always unchecked.

    ===========================================
    Actually, I think there are many possibilities for this issue. You need to
    post some of your implement code for us to check.

    Anyway, I think the likest problem may be that you did not judge the
    postback, and always rebind the Repeater control in Page_Load event.

    If you always rebind your repeater control to the data, then the whole
    status of Repeater control will be refreshed(Initialized), so the
    checkbox's checked status will be lost.

    Normally, if you only bind at the first load time, then everything will
    work well, like this:

    Html code:
    <asp:repeater id="Repeater1" runat="server">
    <HeaderTemplate>
    <table align="center" border="1">
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td align="center"><asp:CheckBox ID="cb"
    Runat="server"></asp:CheckBox></td>
    <td runat="server" id="job_id" align="center"><%#
    DataBinder.Eval(Container.DataItem,"job_id")%></td>
    <td runat="server" id="min_lvl" align="center"><%#
    DataBinder.Eval(Container.DataItem,"min_lvl")%></td>
    <td runat="server" id="max_lvl" align="center"><%#
    DataBinder.Eval(Container.DataItem,"max_lvl")%></td>
    </tr>
    <tr>
    <td runat="server" id="job_desc" align="center" colspan="4"><%#
    DataBinder.Eval(Container.DataItem,"job_desc")%></td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:repeater>

    Then, at the server side:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    SqlDataAdapter adapter=new SqlDataAdapter("select * from
    jobs","server=localhost;database=pubs;uid=sa;pwd=");
    DataSet ds=new DataSet();
    adapter.Fill(ds);
    Repeater1.DataSource=ds.Tables[0];
    Repeater1.DataBind();
    }
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {
    foreach(RepeaterItem ri in Repeater1.Items)
    {

    if(ri.ItemType==ListItemType.Item||ri.ItemType==ListItemType.AlternatingItem
    )
    {
    foreach(Control c in ri.Controls)
    {
    if(c is CheckBox)
    {
    CheckBox cb=(CheckBox)c;
    if(cb.Checked)
    {
    this.Response.Write("The "+ ri.ItemIndex.ToString()+"th item is
    checked<br>");
    }
    }
    }
    }
    }
    }

    Note: I used Sql Server's default "jobs" table in "pubs" database.

    ==============================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.


    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 13, 2004
    #7
  8. Per Salmi

    Per Salmi Guest

    Yes, the problem was that the Repeater databinding was executed before I
    tried to find the checked CheckBoxes... Now that is fixed and I find the
    checked items. I get the IDs of my database items by providing them in
    hidden fields in the repeater and accessing them by the FindControl-method
    as I understand that the RepeaterItem.DataItem is not available until
    databinding has been established.

    So, now it works!

    /Per Salmi

     
    Per Salmi, Feb 13, 2004
    #8
  9. Hi Per,

    Thanks very much for your feedback.

    I am glad it works now, if you still have any further concern, please feel
    free to tell me, I will help you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 16, 2004
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.