DropDownList in Datagrid programatically?

B

Bruce Whitehouse

All the exampes I've seen for populating a datagrid with a DropDownList show
the data grid code in the web form.

I assume its possible to simply have

<asp:DataGrid id="dgName" runat="servver"><asp:DataGrid>

in the web form and programatically build the datagrid (including
DropDownLists). I've got my datagrid built programatically, but can quite
work out how to programatically insert a DropDownList.

Does anyone know, or know of a reference site where this is described?

Many thanks. It'll make my Christmas if I can have this solved!
Bruce
 
M

Micharel Tkachev

You can use event OnItemCreated where "e" returns Item of DataGrid

DropDownList ddl = new DropDownList();
//Your code
e.Cells[0].Controls.Add(ddl)

Or you can create TemplateColumn where will be DropDownList

bye-bye
 
B

Bruce Whitehouse

Micharel

Thanks for your suggestion. I'm a little confused by your reponse though.

I've copied my code below. Can you help?

regards,
Bruce




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 dgTest As System.Web.UI.WebControls.DataGrid

Protected WithEvents ddlTest As System.Web.UI.WebControls.DropDownList

'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

Dim RowView As DataView



Private Function getRows() As ArrayList

Dim arrReturn As New ArrayList

Dim objRow1 As New clsRow

Dim objRow2 As New clsRow

objRow1.RowId = 1

objRow1.DropDownId = 2

objRow1.Name = "One"

arrReturn.Add(objRow1)

objRow2.RowId = 3

objRow2.DropDownId = 4

objRow2.Name = "Two"

arrReturn.Add(objRow2)

Return arrReturn

End Function

ReadOnly Property InvoiceRows() As DataTable

Get

Dim tmpDataTable As New DataTable

Dim dr As DataRow

Dim arrRows As ArrayList

Dim objRow As New clsRow

tmpDataTable.Columns.Add(New DataColumn("RowID", GetType(Integer)))

tmpDataTable.Columns.Add(New DataColumn("DropDown", GetType(DropDownList)))

arrRows = getRows()

For Each objRow In arrRows

Dim ddlTest As New DropDownList

dr = tmpDataTable.NewRow

dr(0) = objRow.RowId

ddlTest.DataSource = CreateDDL(arrRows)

ddlTest.DataValueField = "ValueField"

ddlTest.DataTextField = "TextField"

ddlTest.DataBind()

dr(1) = ddlTest

tmpDataTable.Rows.Add(dr)

Next

Return tmpDataTable

End Get

End Property

Private Function populateDataGrid()

dgTest.DataSource = InvoiceRows

dgTest.DataBind()

End Function

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim arrRows As ArrayList

If Not Page.IsPostBack Then

populateDataGrid()

End If

End Sub



Private Function CreateRow(ByVal Value As String, ByVal Text As String,
ByVal dt As DataTable) As DataRow

Dim dr As DataRow = dt.NewRow()

dr(0) = Value

dr(1) = Text

Return dr

End Function

Private Function CreateDDL(ByVal arrList As ArrayList) As ICollection

Dim dt As DataTable = New DataTable

Dim objRow As clsRow

dt.Columns.Add(New DataColumn("ValueField", GetType(String)))

dt.Columns.Add(New DataColumn("TextField", GetType(String)))

For Each objRow In arrList

dt.Rows.Add(CreateRow(objRow.DropDownId, objRow.Name, dt))

Next

Dim dv As DataView = New DataView(dt)

Return dv

End Function

End Class
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top