Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 27th, 2003, 09:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default Data Grid Drop Down List Column

Hello,

I have a data grid, which has a TemplateColumn, which has a drop-down list for a column:

Code:
<asp:DataGrid ...>
  <asp:TemplateColumn>
    <HeaderTemplate>Proposed Style</HeaderTemplate>
    <ItemTemplate>
     <asp:DropDownList runat="server" DataTextField="StyleName" DataValueField="StyleName"/>
    </ItemTemplate>
  </asp:TemplateColumn>
</asp:DataGrid>
I want to databind a DataTable to all of the columns. How do I do that? I tried to use the Controls property of the data grid, but it can't access this column.

Any ideas?

Thanks,

Brian Mains
__________________
Brian
  #2 (permalink)  
Old October 28th, 2003, 09:36 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Use the DataGrid's DataSource property to bind to a data source:

myDataGrid.DataSource = myDataTable
myDataGrid.DataBind()

To bind data to a DropDownList in one of your grid's columns, you need to make a data bind call for each row that is bound in the data grid. There is an event on the datagrid called ItemDataBound. You need to build a handler for this so that you can bind each row's DropDownList when the row is built. You can access the DropDownList for each row by accessing the controls collection of the appropriate cell of the passed in DataGridItem.

Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
    Dim objDDL As System.Web.UI.WebControls.DropDownList
    objDDL = CType(e.Item.Cells(0).Controls(0), System.Web.UI.WebControls.DropDownList)
    objDDL.DataSource = myDataTable
    objDDL.DataBind()
End Sub

Your Cells(x) index is based on the index from the <Columns> part of the DataGrid control starting from zero. Unless you have more than one server control in a column, the Controls(x) index should be 0.

Be very aware of when you load the DataTable. This event handler will happen for EACH item of bound data, so you will want to load the data only ONCE and before you call the grid's DataBind(). Then you can bind the same table to each DropDownList. Usually what I do is build a method that I call to bind the DataGrid. In there I make the calls to actually get the data, then set the DataSource and call DataBind(). You could then add in the code to load the DataTable for your DropDownList. You'll have to setup the DataTable as a class scope variable so you can then use it in the ItemDataBound handler. (Alternatively, you could set up the DataTable as a static object within the ItemDataBound handler and only load it if it's not Nothing. This would be a little cleaner and would keep the appropriate code together.)

Peter
  #3 (permalink)  
Old September 20th, 2004, 08:57 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello Peter,
about static DataTable object you mentioned here,I've tested my program,I realized that my Application runs much slower,(I could feel it!!!)
I think if we just use a global DataTable and every time fill it through our Connection object,our Application could run faster,
Dont you think it could be better and faster?

--------------------------------------------
Mehdi.:)
  #4 (permalink)  
Old September 20th, 2004, 08:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Or use the Cache to retain the data table. Cache is pretty fast.

Brian
  #5 (permalink)  
Old September 21st, 2004, 08:39 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

yes great solution,
I used an ArrayList instead of a DataTable for populating the DropDownList,
in my Cache object ...
Thank you.

--------------------------------------------
Mehdi.:)
 


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
multi column database source for drop down list mii2029 ASP.NET 3.5 Basics 3 August 4th, 2008 05:05 PM
having some fields of a grid view a drop list dsblack@montgomerycountyt ASP.NET 2.0 Professional 0 June 2nd, 2006 04:18 PM
How to sort a column in data grid: kenn_rosie ASP.NET 1.0 and 1.1 Basics 1 January 26th, 2006 01:03 PM
Drop Down Combo in Data Grid wwwdirzala VB Databases Basics 0 May 24th, 2004 04:44 AM



All times are GMT -4. The time now is 12:30 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.