DataList with 2 columns, new Header row when catagory field in DataRow changes, is this possisble?

DataList with 2 columns, new Header row when catagory field in DataRow changes, is this possisble?

Post by Patrich Lyn » Sun, 20 Apr 2003 10:42:16



I am trying to display a two column Datalist of checkbox for products.
 Simple enough, but the data set populating the checklist also has a
Product type which it is grouped on and when the Product type changes,
I want to display that new product type name as a header row. Like
below:

Baby Care
[] Skin Powder      [] Moisterizer
[] Sun Screen

Body Care
[] Elbow Greese     [] Ultra Softener
[] Cheek brigthener [] Bath Oil

I almost got by doing:
<asp:datalist id=DataList1 RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
 <ItemTemplate>
  <asp:label id="GroupHead" runat="server"/>
  <asp:CheckBox runat="server" ID="chbxProduct"
Text='<%#DataBinder.Eval(Container.DataItem,
"Name")%>'></asp:CheckBox>
 <input id="productId" type="hidden" name="productId" runat="server"
value='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>'>
 </ItemTemplate>
</asp:datalist>

and then in the ItemCreated Event:
public void DataList1_ItemCreated(Object sender, DataListItemEventArgs
e)
{
 Label lbGroupHead ;
 // counter declared in page code behind to count num. of rows
 NumDataListItems = NumDataListItems + 1 ;      
 string sCategory ;
 if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
   {
    //18th field is the Catagory field, CurrCatagory delcared in pge
code behind
    // to hold current catagory between each datarow
    sCategory = ((DataRowView)e.Item.DataItem).Row.ItemArray[18].ToString();
    if (sCategory != CurrCatagory)
      {
        // get the label where we put the group heading
       lbGroupHead = (Label)e.Item.FindControl("GroupHead");
       // we are one the second column, finish row and start another.
       if (NumDataListItems % 2 == 0)
        {
          lbGroupHead.Text = "&nbsp;</TD></TR><TR><TD><span
class='sampleheader'>"
          + sCategory + "</span></TD><TD>&nbsp;</TD></TR><TR><TD>" ;
        }
       else // we are one the first column, just put in the header
        {
         lbGroupHead.Text = "<span class='sampleheader'>"
         + sCategory + "</span></TD><TD>&nbsp;</TD></TR><TR><TD>";
        }
       CurrCatagory = sCategory ;
      }
   }    

Quote:}

This got me so close but fails to line up when there are an odd number
of products in a particular group.  I can write out a the closing TD
and TR, but the Datalist control "knowns" it's only done 1 column and
so the next  item is treated as the second column and another "</TR>"
get written after and I get this:

Baby Care
[] Skin Powder      [] Moisterizer
[] Sun Screen

Body Care
[] Elbow Greese                 <-- Oops, DataList known this is '2nd'
column
[] Ultra Softener   [] Cheek brigthener
[] Bath Oil

It there a way to do this?  Ideally, it there a way to tell the
DataList, "Hey, Finish this Row, I want to start a new row!"  Maybe
another approach?  If I only had 1 column, no problem.  Any ideas???
TIA!

 
 
 

1. add new row above datagrid column header row?

How can I add a new row (in ItemCreated for the ListItemType.Header)
so that it will be above the column headings for each row?

I can add a cell, and I've tried adding a cell with a ColumnSpan set
to the number of columns - but it always creates an entirely new
column.

I have not figured out how to add a new row, and add it above the
existing row containing the column headers.
--
Thanks in advance, Les Caudle

2. How test from DOS program Windows version if running?

3. Can we create DataList Row Headers???

4. L(in)ucida

5. DataRow.AcceptChanges, Simple Data Binding: causes wrong row to change

6. Export Gnatt Chart to HTML

7. Update requires a valid InsertCommand when passed DataRow collection with new rows...

8. DEC Station 2100 help

9. How to insert a new row in a database using NewRow method of a DataRow object