Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Programmatically Adding An Item To The DataList

Reply
Thread Tools

Programmatically Adding An Item To The DataList

 
 
Umut K.
Guest
Posts: n/a
 
      09-06-2003
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


 
Reply With Quote
 
 
 
 
Bench Wang
Guest
Posts: n/a
 
      09-06-2003
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.


 
Reply With Quote
 
 
 
 
Umut K.
Guest
Posts: n/a
 
      09-06-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      09-06-2003
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." <(E-Mail Removed)> wrote in message
news:udKD%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Umut K.
Guest
Posts: n/a
 
      09-07-2003
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]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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." <(E-Mail Removed)> wrote in message
> news:udKD%(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      09-07-2003
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." <(E-Mail Removed)> wrote in message
news:%234$(E-Mail Removed)...
> 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]" <(E-Mail Removed)> wrote in

message
> news:%(E-Mail Removed)...
> > 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." <(E-Mail Removed)> wrote in message
> > news:udKD%(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Umut K.
Guest
Posts: n/a
 
      09-07-2003
Jay, thanks. i'm going to try your suggestions..


"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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." <(E-Mail Removed)> wrote in message
> news:%234$(E-Mail Removed)...
> > 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]" <(E-Mail Removed)> wrote in

> message
> > news:%(E-Mail Removed)...
> > > 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." <(E-Mail Removed)> wrote in message
> > > news:udKD%(E-Mail Removed)...
> > > > 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
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WebControl - CollectionEditor Problem. Changing id property of new added collection item causes not adding item to collection - Sergio ASP .Net Web Controls 0 05-29-2006 06:20 AM
Adding another item to a completed DataLIst David Lozzi ASP .Net Building Controls 5 10-19-2005 01:13 AM
Setting up a datalist control - Item_DataBound for a datalist in a datalist Nevyn Twyll ASP .Net 8 09-09-2004 10:13 PM
Programmatically Adding An Item To The DataList Umut K. ASP .Net 6 09-07-2003 02:28 PM
Programmatically Adding An Item To The DataList Umut K. ASP .Net Web Controls 0 09-06-2003 02:16 AM



Advertisments