|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
October 27th, 2003, 10:23 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
October 28th, 2003, 10:36 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|
September 20th, 2004, 08:57 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.:)
|
September 20th, 2004, 08:18 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
Or use the Cache to retain the data table. Cache is pretty fast.
Brian
|
September 21st, 2004, 08:39 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
yes great solution,
I used an ArrayList instead of a DataTable for populating the DropDownList,
in my Cache object ...
Thank you.
--------------------------------------------
Mehdi.:)
|
|
|