Programmatically Adding An Item To The DataList

Discussion in 'ASP .Net Datagrid Control' started by Umut K., Sep 6, 2003.

  1. Umut K.

    Umut K. Guest

    Hi all,
    I've a DataList control and it's DataBound by a SqlDataReader...
    The Reader returns say 3 records and as expected the datalist shows 3 rows.
    What i want to do is to add another extra Record (which is not neccesarily a
    database record..) but it'll be shown up as an ordinary item in the datalist
    rendered as an Item or AlternatingItem ...
    Is there a way to do this?

    Thanks
    Umut K., Sep 6, 2003
    #1
    1. Advertising

  2. Umut K.

    Bench Wang Guest

    Try to fill DataList with a loop and then add the extra record.
    Or override PreRender event of DataList and add the extra record.
    Hope this helps.
    Bench Wang, Sep 6, 2003
    #2
    1. Advertising

  3. Umut K.

    Umut K. Guest

    Thank you,
    That's what i'm avoiding to do... (but seems like the only way i have .. )
    Why isn't it allowed myDataList.Items.Add(new DataListItem()...... ) or
    smtg. like this..
    ??
    Any other ideas?

    "Bench Wang" <> wrote in message
    news:...
    > Try to fill DataList with a loop and then add the extra record.
    > Or override PreRender event of DataList and add the extra record.
    > Hope this helps.
    >
    >
    Umut K., Sep 6, 2003
    #3
  4. Umut,
    For a System.Web.UI.WebControls.DropDownList, I use something similar to:

    Dim Data As DataSet
    Dim lst As DropDownList
    lst.DataMember = "MyMember"
    lst.DataTextField = "FieldName"
    lst.DataSrouce = Data

    DataBind()

    lst.Items.Insert(0, New ListItem("<select item>", ""))

    I use the above in the Page Load event, which adds the <select item> to the
    top of the drop down list. Notice that I call DataBind before I add the
    extra item.

    I would expect a DataList to work the same way.

    Hope this helps
    Jay

    "Umut K." <> wrote in message
    news:udKD%...
    > Hi all,
    > I've a DataList control and it's DataBound by a SqlDataReader...
    > The Reader returns say 3 records and as expected the datalist shows 3

    rows.
    > What i want to do is to add another extra Record (which is not neccesarily

    a
    > database record..) but it'll be shown up as an ordinary item in the

    datalist
    > rendered as an Item or AlternatingItem ...
    > Is there a way to do this?
    >
    > Thanks
    >
    >
    Jay B. Harlow [MVP - Outlook], Sep 6, 2003
    #4
  5. Umut K.

    Umut K. Guest

    Yes, while this works great for the DropDownList,
    System.Web.UI.WebControls.DataList doesn't allow sucha method like Insert...
    I mean ther's no method like Insert or Add (or am i missing something?)

    "Jay B. Harlow [MVP - Outlook]" <> wrote in message
    news:%...
    > Umut,
    > For a System.Web.UI.WebControls.DropDownList, I use something similar to:
    >
    > Dim Data As DataSet
    > Dim lst As DropDownList
    > lst.DataMember = "MyMember"
    > lst.DataTextField = "FieldName"
    > lst.DataSrouce = Data
    >
    > DataBind()
    >
    > lst.Items.Insert(0, New ListItem("<select item>", ""))
    >
    > I use the above in the Page Load event, which adds the <select item> to

    the
    > top of the drop down list. Notice that I call DataBind before I add the
    > extra item.
    >
    > I would expect a DataList to work the same way.
    >
    > Hope this helps
    > Jay
    >
    > "Umut K." <> wrote in message
    > news:udKD%...
    > > Hi all,
    > > I've a DataList control and it's DataBound by a SqlDataReader...
    > > The Reader returns say 3 records and as expected the datalist shows 3

    > rows.
    > > What i want to do is to add another extra Record (which is not

    neccesarily
    > a
    > > database record..) but it'll be shown up as an ordinary item in the

    > datalist
    > > rendered as an Item or AlternatingItem ...
    > > Is there a way to do this?
    > >
    > > Thanks
    > >
    > >

    >
    >
    Umut K., Sep 7, 2003
    #5
  6. Umut,
    Doh! I don't see an Add or Insert on the DataListItemCollection either.

    Curious that they are different, must be some reason. (shrug)

    Two options I would consider instead then.

    1. Use the SqlDataReader to populate a DataTable, add your row to the
    DataTable, then bind to the DataTable.
    2. Create a 'Proxy' Data reader that returns the values out of a contained
    SqlDataReader, plus the extra row. This class would implement the
    IDataReader, IDataRecord, IDisposable, and IEnumerable interfaces.
    Delegating most calls to the contained SqlDataReader, however it would
    return one or two records first or last... Similar to how the
    System.IO.Stream classes work, where you can chain one stream to second
    stream chained to a third stream...

    I haven't done a lot with DataLists, maybe there is an easier way.

    Hope this helps
    Jay

    "Umut K." <> wrote in message
    news:%234$...
    > Yes, while this works great for the DropDownList,
    > System.Web.UI.WebControls.DataList doesn't allow sucha method like

    Insert...
    > I mean ther's no method like Insert or Add (or am i missing something?)
    >
    > "Jay B. Harlow [MVP - Outlook]" <> wrote in

    message
    > news:%...
    > > Umut,
    > > For a System.Web.UI.WebControls.DropDownList, I use something similar

    to:
    > >
    > > Dim Data As DataSet
    > > Dim lst As DropDownList
    > > lst.DataMember = "MyMember"
    > > lst.DataTextField = "FieldName"
    > > lst.DataSrouce = Data
    > >
    > > DataBind()
    > >
    > > lst.Items.Insert(0, New ListItem("<select item>", ""))
    > >
    > > I use the above in the Page Load event, which adds the <select item> to

    > the
    > > top of the drop down list. Notice that I call DataBind before I add the
    > > extra item.
    > >
    > > I would expect a DataList to work the same way.
    > >
    > > Hope this helps
    > > Jay
    > >
    > > "Umut K." <> wrote in message
    > > news:udKD%...
    > > > Hi all,
    > > > I've a DataList control and it's DataBound by a SqlDataReader...
    > > > The Reader returns say 3 records and as expected the datalist shows 3

    > > rows.
    > > > What i want to do is to add another extra Record (which is not

    > neccesarily
    > > a
    > > > database record..) but it'll be shown up as an ordinary item in the

    > > datalist
    > > > rendered as an Item or AlternatingItem ...
    > > > Is there a way to do this?
    > > >
    > > > Thanks
    > > >
    > > >

    > >
    > >

    >
    >
    Jay B. Harlow [MVP - Outlook], Sep 7, 2003
    #6
  7. Umut K.

    Umut K. Guest

    Jay, thanks. i'm going to try your suggestions..


    "Jay B. Harlow [MVP - Outlook]" <> wrote in message
    news:...
    > Umut,
    > Doh! I don't see an Add or Insert on the DataListItemCollection either.
    >
    > Curious that they are different, must be some reason. (shrug)
    >
    > Two options I would consider instead then.
    >
    > 1. Use the SqlDataReader to populate a DataTable, add your row to the
    > DataTable, then bind to the DataTable.
    > 2. Create a 'Proxy' Data reader that returns the values out of a contained
    > SqlDataReader, plus the extra row. This class would implement the
    > IDataReader, IDataRecord, IDisposable, and IEnumerable interfaces.
    > Delegating most calls to the contained SqlDataReader, however it would
    > return one or two records first or last... Similar to how the
    > System.IO.Stream classes work, where you can chain one stream to second
    > stream chained to a third stream...
    >
    > I haven't done a lot with DataLists, maybe there is an easier way.
    >
    > Hope this helps
    > Jay
    >
    > "Umut K." <> wrote in message
    > news:%234$...
    > > Yes, while this works great for the DropDownList,
    > > System.Web.UI.WebControls.DataList doesn't allow sucha method like

    > Insert...
    > > I mean ther's no method like Insert or Add (or am i missing something?)
    > >
    > > "Jay B. Harlow [MVP - Outlook]" <> wrote in

    > message
    > > news:%...
    > > > Umut,
    > > > For a System.Web.UI.WebControls.DropDownList, I use something similar

    > to:
    > > >
    > > > Dim Data As DataSet
    > > > Dim lst As DropDownList
    > > > lst.DataMember = "MyMember"
    > > > lst.DataTextField = "FieldName"
    > > > lst.DataSrouce = Data
    > > >
    > > > DataBind()
    > > >
    > > > lst.Items.Insert(0, New ListItem("<select item>", ""))
    > > >
    > > > I use the above in the Page Load event, which adds the <select item>

    to
    > > the
    > > > top of the drop down list. Notice that I call DataBind before I add

    the
    > > > extra item.
    > > >
    > > > I would expect a DataList to work the same way.
    > > >
    > > > Hope this helps
    > > > Jay
    > > >
    > > > "Umut K." <> wrote in message
    > > > news:udKD%...
    > > > > Hi all,
    > > > > I've a DataList control and it's DataBound by a SqlDataReader...
    > > > > The Reader returns say 3 records and as expected the datalist shows

    3
    > > > rows.
    > > > > What i want to do is to add another extra Record (which is not

    > > neccesarily
    > > > a
    > > > > database record..) but it'll be shown up as an ordinary item in the
    > > > datalist
    > > > > rendered as an Item or AlternatingItem ...
    > > > > Is there a way to do this?
    > > > >
    > > > > Thanks
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Umut K., Sep 7, 2003
    #7
    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. John Criswell
    Replies:
    1
    Views:
    1,355
    John Criswell
    Jul 24, 2003
  2. Umut K.
    Replies:
    6
    Views:
    1,026
    Umut K.
    Sep 7, 2003
  3. Nevyn Twyll
    Replies:
    8
    Views:
    1,849
  4. David Lozzi

    Adding another item to a completed DataLIst

    David Lozzi, Oct 10, 2005, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    165
    Steven Cheng[MSFT]
    Oct 19, 2005
  5. Umut K.

    Programmatically Adding An Item To The DataList

    Umut K., Sep 6, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    104
    Umut K.
    Sep 6, 2003
Loading...

Share This Page