Databind dropdown in datagrid

  • Thread starter Søren M. Olesen
  • Start date
S

Søren M. Olesen

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
 
E

Earl Teigrob

Is this what you are looking for?

ManagerId2.Items.FindByValue(pkManager).Selected=true;

Where ManagerId2 is the dropdownlist
 
S

Søren M. Olesen

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
 
E

Earl Teigrob

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top