Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Databind dropdown in datagrid

Reply
Thread Tools

Databind dropdown in datagrid

 
 
Søren M. Olesen
Guest
Posts: n/a
 
      02-04-2004
Hi
(Sorry if this has meen answered a million times before...I just haven't
been able to find the answer)

I'm trying to dynamically create a datagrid containing a cell as a dropdown.
I seem to get both the datagrid and the dropdown populated (see code below)
with the correct values, however I haven't yet been able to get the selected
value in the dropdown to match the value of the cell behind.

It seems like I have to set the selected value in the OnDataLoad event,
however I'm not able to get the value from the cell behind in
OnDataLoad...so what't the trix ??

TIA

Søren

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public Class myDDTemplate
Implements ITemplate
Private lov As ArrayList
Private attribute As String
Sub New(ByVal l As ArrayList, ByVal a As String)
lov = l
Attribute = a
End Sub
Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
If Not IsNothing(lov) Then
Dim dl As New DropDownList
dl.AutoPostBack = False
AddHandler dl.DataBinding, AddressOf Me.OnBindData
AddHandler dl.Load, AddressOf Me.OnDataLoad
dl.ID = attribute
dl.DataSource = lov
dl.DataBind()
container.Controls.Add(dl)
End If
End Sub
Sub OnDataLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dd As DropDownList = CType(sender, DropDownList)
Dim container As DataGridItem
If Not IsNothing(dd.NamingContainer) Then
container = CType(dd.NamingContainer, DataGridItem)
If Not IsNothing(container) Then
Dim value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
End If
End If
End Sub
Sub OnBindData(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dd As DropDownList = CType(sender, DropDownList)
Dim container As DataGridItem
If Not IsNothing(dd.NamingContainer) Then
container = CType(dd.NamingContainer, DataGridItem)
If Not IsNothing(container) Then
Dim value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
End If
End If
End Sub
End Class

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim attribute As String = "DXP"

Dim lov As ArrayList = New ArrayList
For i As Integer = 0 To 5
lov.Add("Item" & i)
Next

Dim dt As DataTable = New DataTable
dt.Columns.Add(New DataColumn(attribute, GetType(String)))
Dim dr As DataRow = dt.NewRow()
dr(0) = "Item5"
dt.Rows.Add(dr)
Dim dv As DataView = New DataView(dt)

DataGrid1.AutoGenerateColumns = False
Dim tc1 As New TemplateColumn
tc1.ItemTemplate = New myDDTemplate(lov, attribute)
tc1.HeaderText = attribute
DataGrid1.Columns.Add(tc1)
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub
End Class





 
Reply With Quote
 
 
 
 
Earl Teigrob
Guest
Posts: n/a
 
      02-04-2004
Is this what you are looking for?

ManagerId2.Items.FindByValue(pkManager).Selected=t rue;

Where ManagerId2 is the dropdownlist

"Søren M. Olesen" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi
> (Sorry if this has meen answered a million times before...I just haven't
> been able to find the answer)
>
> I'm trying to dynamically create a datagrid containing a cell as a

dropdown.
> I seem to get both the datagrid and the dropdown populated (see code

below)
> with the correct values, however I haven't yet been able to get the

selected
> value in the dropdown to match the value of the cell behind.
>
> It seems like I have to set the selected value in the OnDataLoad event,
> however I'm not able to get the value from the cell behind in
> OnDataLoad...so what't the trix ??
>
> TIA
>
> Søren
>
> Public Class WebForm1
> Inherits System.Web.UI.Page
>
> #Region " Web Form Designer Generated Code "
>
> 'This call is required by the Web Form Designer.
> <System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()
>
> End Sub
> Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
>
> 'NOTE: The following placeholder declaration is required by the Web Form
> Designer.
> 'Do not delete or move it.
> Private designerPlaceholderDeclaration As System.Object
>
> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
> 'CODEGEN: This method call is required by the Web Form Designer
> 'Do not modify it using the code editor.
> InitializeComponent()
> End Sub
>
> #End Region
>
> Public Class myDDTemplate
> Implements ITemplate
> Private lov As ArrayList
> Private attribute As String
> Sub New(ByVal l As ArrayList, ByVal a As String)
> lov = l
> Attribute = a
> End Sub
> Sub InstantiateIn(ByVal container As Control) Implements
> ITemplate.InstantiateIn
> If Not IsNothing(lov) Then
> Dim dl As New DropDownList
> dl.AutoPostBack = False
> AddHandler dl.DataBinding, AddressOf Me.OnBindData
> AddHandler dl.Load, AddressOf Me.OnDataLoad
> dl.ID = attribute
> dl.DataSource = lov
> dl.DataBind()
> container.Controls.Add(dl)
> End If
> End Sub
> Sub OnDataLoad(ByVal sender As Object, ByVal e As System.EventArgs)
> Dim dd As DropDownList = CType(sender, DropDownList)
> Dim container As DataGridItem
> If Not IsNothing(dd.NamingContainer) Then
> container = CType(dd.NamingContainer, DataGridItem)
> If Not IsNothing(container) Then
> Dim value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
> End If
> End If
> End Sub
> Sub OnBindData(ByVal sender As Object, ByVal e As System.EventArgs)
> Dim dd As DropDownList = CType(sender, DropDownList)
> Dim container As DataGridItem
> If Not IsNothing(dd.NamingContainer) Then
> container = CType(dd.NamingContainer, DataGridItem)
> If Not IsNothing(container) Then
> Dim value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
> End If
> End If
> End Sub
> End Class
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> 'Put user code to initialize the page here
> Dim attribute As String = "DXP"
>
> Dim lov As ArrayList = New ArrayList
> For i As Integer = 0 To 5
> lov.Add("Item" & i)
> Next
>
> Dim dt As DataTable = New DataTable
> dt.Columns.Add(New DataColumn(attribute, GetType(String)))
> Dim dr As DataRow = dt.NewRow()
> dr(0) = "Item5"
> dt.Rows.Add(dr)
> Dim dv As DataView = New DataView(dt)
>
> DataGrid1.AutoGenerateColumns = False
> Dim tc1 As New TemplateColumn
> tc1.ItemTemplate = New myDDTemplate(lov, attribute)
> tc1.HeaderText = attribute
> DataGrid1.Columns.Add(tc1)
> DataGrid1.DataSource = dv
> DataGrid1.DataBind()
> End Sub
> End Class
>
>
>
>
>



 
Reply With Quote
 
 
 
 
Søren M. Olesen
Guest
Posts: n/a
 
      02-05-2004
No, not really.

What I'm trying to do, is have a cell in a DataGrid as a DropDownList. I',
populate the DataGrid with a DataView, and I'd like the selected value in
the DropDown to match the value of the cell. However I simply can't figure
out how to get the value from the cell, so I can set the selected value in
the DropDownList

Regards,

Søren




"Earl Teigrob" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Is this what you are looking for?
>
> ManagerId2.Items.FindByValue(pkManager).Selected=t rue;
>
> Where ManagerId2 is the dropdownlist
>
> "Søren M. Olesen" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hi
> > (Sorry if this has meen answered a million times before...I just haven't
> > been able to find the answer)
> >
> > I'm trying to dynamically create a datagrid containing a cell as a

> dropdown.
> > I seem to get both the datagrid and the dropdown populated (see code

> below)
> > with the correct values, however I haven't yet been able to get the

> selected
> > value in the dropdown to match the value of the cell behind.
> >
> > It seems like I have to set the selected value in the OnDataLoad event,
> > however I'm not able to get the value from the cell behind in
> > OnDataLoad...so what't the trix ??
> >
> > TIA
> >
> > Søren
> >
> > Public Class WebForm1
> > Inherits System.Web.UI.Page
> >
> > #Region " Web Form Designer Generated Code "
> >
> > 'This call is required by the Web Form Designer.
> > <System.Diagnostics.DebuggerStepThrough()> Private Sub

> InitializeComponent()
> >
> > End Sub
> > Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
> >
> > 'NOTE: The following placeholder declaration is required by the Web Form
> > Designer.
> > 'Do not delete or move it.
> > Private designerPlaceholderDeclaration As System.Object
> >
> > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles MyBase.Init
> > 'CODEGEN: This method call is required by the Web Form Designer
> > 'Do not modify it using the code editor.
> > InitializeComponent()
> > End Sub
> >
> > #End Region
> >
> > Public Class myDDTemplate
> > Implements ITemplate
> > Private lov As ArrayList
> > Private attribute As String
> > Sub New(ByVal l As ArrayList, ByVal a As String)
> > lov = l
> > Attribute = a
> > End Sub
> > Sub InstantiateIn(ByVal container As Control) Implements
> > ITemplate.InstantiateIn
> > If Not IsNothing(lov) Then
> > Dim dl As New DropDownList
> > dl.AutoPostBack = False
> > AddHandler dl.DataBinding, AddressOf Me.OnBindData
> > AddHandler dl.Load, AddressOf Me.OnDataLoad
> > dl.ID = attribute
> > dl.DataSource = lov
> > dl.DataBind()
> > container.Controls.Add(dl)
> > End If
> > End Sub
> > Sub OnDataLoad(ByVal sender As Object, ByVal e As System.EventArgs)
> > Dim dd As DropDownList = CType(sender, DropDownList)
> > Dim container As DataGridItem
> > If Not IsNothing(dd.NamingContainer) Then
> > container = CType(dd.NamingContainer, DataGridItem)
> > If Not IsNothing(container) Then
> > Dim value As String = CStr(DataBinder.Eval(container.DataItem,

attribute))
> > End If
> > End If
> > End Sub
> > Sub OnBindData(ByVal sender As Object, ByVal e As System.EventArgs)
> > Dim dd As DropDownList = CType(sender, DropDownList)
> > Dim container As DataGridItem
> > If Not IsNothing(dd.NamingContainer) Then
> > container = CType(dd.NamingContainer, DataGridItem)
> > If Not IsNothing(container) Then
> > Dim value As String = CStr(DataBinder.Eval(container.DataItem,

attribute))
> > End If
> > End If
> > End Sub
> > End Class
> >
> > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles MyBase.Load
> > 'Put user code to initialize the page here
> > Dim attribute As String = "DXP"
> >
> > Dim lov As ArrayList = New ArrayList
> > For i As Integer = 0 To 5
> > lov.Add("Item" & i)
> > Next
> >
> > Dim dt As DataTable = New DataTable
> > dt.Columns.Add(New DataColumn(attribute, GetType(String)))
> > Dim dr As DataRow = dt.NewRow()
> > dr(0) = "Item5"
> > dt.Rows.Add(dr)
> > Dim dv As DataView = New DataView(dt)
> >
> > DataGrid1.AutoGenerateColumns = False
> > Dim tc1 As New TemplateColumn
> > tc1.ItemTemplate = New myDDTemplate(lov, attribute)
> > tc1.HeaderText = attribute
> > DataGrid1.Columns.Add(tc1)
> > DataGrid1.DataSource = dv
> > DataGrid1.DataBind()
> > End Sub
> > End Class
> >
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Earl Teigrob
Guest
Posts: n/a
 
      02-05-2004
In the ItemDataBound event, you can retrieve the currently bounding row of
the view and get its values like this
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{

if(e.Item.ItemType == ListItemType.EditItem)

{

DataRowView drv = (DataRowView) e.Item.DataItem;

String currentgenre = drv["pkManager"].ToString();

....

drv now had the current row form which you can retieve any data in that row.

Hope that helps

Earl



"Søren M. Olesen" <(E-Mail Removed)> wrote in message
news:uT01T$(E-Mail Removed)...
> No, not really.
>
> What I'm trying to do, is have a cell in a DataGrid as a DropDownList. I',
> populate the DataGrid with a DataView, and I'd like the selected value in
> the DropDown to match the value of the cell. However I simply can't figure
> out how to get the value from the cell, so I can set the selected value in
> the DropDownList
>
> Regards,
>
> Søren
>
>
>
>
> "Earl Teigrob" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Is this what you are looking for?
> >
> > ManagerId2.Items.FindByValue(pkManager).Selected=t rue;
> >
> > Where ManagerId2 is the dropdownlist
> >
> > "Søren M. Olesen" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > Hi
> > > (Sorry if this has meen answered a million times before...I just

haven't
> > > been able to find the answer)
> > >
> > > I'm trying to dynamically create a datagrid containing a cell as a

> > dropdown.
> > > I seem to get both the datagrid and the dropdown populated (see code

> > below)
> > > with the correct values, however I haven't yet been able to get the

> > selected
> > > value in the dropdown to match the value of the cell behind.
> > >
> > > It seems like I have to set the selected value in the OnDataLoad

event,
> > > however I'm not able to get the value from the cell behind in
> > > OnDataLoad...so what't the trix ??
> > >
> > > TIA
> > >
> > > Søren
> > >
> > > Public Class WebForm1
> > > Inherits System.Web.UI.Page
> > >
> > > #Region " Web Form Designer Generated Code "
> > >
> > > 'This call is required by the Web Form Designer.
> > > <System.Diagnostics.DebuggerStepThrough()> Private Sub

> > InitializeComponent()
> > >
> > > End Sub
> > > Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
> > >
> > > 'NOTE: The following placeholder declaration is required by the Web

Form
> > > Designer.
> > > 'Do not delete or move it.
> > > Private designerPlaceholderDeclaration As System.Object
> > >
> > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> > > System.EventArgs) Handles MyBase.Init
> > > 'CODEGEN: This method call is required by the Web Form Designer
> > > 'Do not modify it using the code editor.
> > > InitializeComponent()
> > > End Sub
> > >
> > > #End Region
> > >
> > > Public Class myDDTemplate
> > > Implements ITemplate
> > > Private lov As ArrayList
> > > Private attribute As String
> > > Sub New(ByVal l As ArrayList, ByVal a As String)
> > > lov = l
> > > Attribute = a
> > > End Sub
> > > Sub InstantiateIn(ByVal container As Control) Implements
> > > ITemplate.InstantiateIn
> > > If Not IsNothing(lov) Then
> > > Dim dl As New DropDownList
> > > dl.AutoPostBack = False
> > > AddHandler dl.DataBinding, AddressOf Me.OnBindData
> > > AddHandler dl.Load, AddressOf Me.OnDataLoad
> > > dl.ID = attribute
> > > dl.DataSource = lov
> > > dl.DataBind()
> > > container.Controls.Add(dl)
> > > End If
> > > End Sub
> > > Sub OnDataLoad(ByVal sender As Object, ByVal e As System.EventArgs)
> > > Dim dd As DropDownList = CType(sender, DropDownList)
> > > Dim container As DataGridItem
> > > If Not IsNothing(dd.NamingContainer) Then
> > > container = CType(dd.NamingContainer, DataGridItem)
> > > If Not IsNothing(container) Then
> > > Dim value As String = CStr(DataBinder.Eval(container.DataItem,

> attribute))
> > > End If
> > > End If
> > > End Sub
> > > Sub OnBindData(ByVal sender As Object, ByVal e As System.EventArgs)
> > > Dim dd As DropDownList = CType(sender, DropDownList)
> > > Dim container As DataGridItem
> > > If Not IsNothing(dd.NamingContainer) Then
> > > container = CType(dd.NamingContainer, DataGridItem)
> > > If Not IsNothing(container) Then
> > > Dim value As String = CStr(DataBinder.Eval(container.DataItem,

> attribute))
> > > End If
> > > End If
> > > End Sub
> > > End Class
> > >
> > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> > > System.EventArgs) Handles MyBase.Load
> > > 'Put user code to initialize the page here
> > > Dim attribute As String = "DXP"
> > >
> > > Dim lov As ArrayList = New ArrayList
> > > For i As Integer = 0 To 5
> > > lov.Add("Item" & i)
> > > Next
> > >
> > > Dim dt As DataTable = New DataTable
> > > dt.Columns.Add(New DataColumn(attribute, GetType(String)))
> > > Dim dr As DataRow = dt.NewRow()
> > > dr(0) = "Item5"
> > > dt.Rows.Add(dr)
> > > Dim dv As DataView = New DataView(dt)
> > >
> > > DataGrid1.AutoGenerateColumns = False
> > > Dim tc1 As New TemplateColumn
> > > tc1.ItemTemplate = New myDDTemplate(lov, attribute)
> > > tc1.HeaderText = attribute
> > > DataGrid1.Columns.Add(tc1)
> > > DataGrid1.DataSource = dv
> > > DataGrid1.DataBind()
> > > End Sub
> > > End Class
> > >
> > >
> > >
> > >
> > >

> >
> >

>
>



 
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
Dropdown in ASCX, when to databind? cmay ASP .Net 2 12-12-2006 05:27 PM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
Dropdown databind to public enum wapsiii ASP .Net 2 10-22-2005 02:43 PM
Databind of large SqlDataReader to dropdown hangs when firewall enabled yanky ASP .Net 1 06-08-2005 03:22 PM
BUG in DataBind? After .DataBind there are more DataGrid Items than DataSet Rows! Michael ASP .Net Datagrid Control 0 12-29-2003 07:47 PM



Advertisments