PDA

View Full Version : about drop down list in a grid view


merlykarl
06-06-2008, 08:10 AM
I m writting a program using VB in asp.net. In my grid view, there is a column contain drop down list. All the drop down list should contain value from one of the column from one of the table in database. I'm now using stored procedure. How can i set value into the drop down list?
The stored procedure named PartNumber.
I usually write dropdownlist.DataTextField = "Column name"
But it's seem fail to work this way cause the dropdown list is now inside a grid view. How can i know the ID for this drop down list?
Somebody please help....

vinyl-junkie
06-06-2008, 01:11 PM
How can i know the ID for this drop down list?

When populating the gridview, make the ID a hidden field.

merlykarl
06-10-2008, 04:38 AM
Sorry, i forgot to tell you that my program is not using sqlDataSource. So how should i done it?I tried this but it didn't work:
Dim ddlPartNo2 As DropDownList = DirectCast(gvPartsOrdering.Row(0).FindControl("ddlPartNo2"), DropDownList)
ddlPartNo2.Items.Add("HELLO WORLD!")

kokhooi
06-11-2008, 06:03 AM
Sample :

HTML Code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<fieldset>
<legend>GridviewNestingDropdowlist</legend>
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="False" onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:boundfield datafield="user_email" headertext="email" />
<asp:boundfield datafield="user_name" headertext="user name" />
<asp:BoundField DataField="user_id" HeaderText="user id" />
<asp:templatefield headertext="products">
<itemtemplate>
<asp:dropdownlist id="dropdownlist1" runat="server">
</asp:dropdownlist>
</itemtemplate>
</asp:templatefield>
</columns>
</fieldset>
</form>
</body>
</html>

Behind Code

Imports System.Data.SqlClient
Imports System.Data

Partial Class _Default
Inherits System.Web.UI.Page
Dim dt As New System.Data.DataTable()

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GridView1.DataSource = getdataset().Tables(0)
GridView1.DataBind()

End Sub

Private Function getdataset() As Data.DataSet
' Dim connectionstring As String = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password="
Dim connectionstring As String = " Your Connection "
Dim query As String = " Your query "
Dim myconnection As New System.Data.SqlClient.SqlConnection(connectionstring)
Dim ad As New System.Data.SqlClient.SqlDataAdapter(query, myconnection)
Dim ds As New Data.DataSet()
ad.Fill(ds)
Return ds
End Function

Protected Sub gridview1_rowdatabound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview1.RowDataBound
Dim mytable As New Data.DataTable()
Dim productidcolumn As New Data.DataColumn("user_id")
Dim productnamecolumn As New Data.DataColumn("user_name")
mytable.Columns.Add(productidcolumn)
mytable.Columns.Add(productnamecolumn)
Dim ds As New Data.DataSet()
ds = getdataset()
Dim categoryid As String = ""
Dim expression As String = String.Empty
If e.Row.RowType = DataControlRowType.DataRow Then

expression = " user_email like '%W%'"
Dim ddl As DropDownList = CType(e.Row.FindControl("dropdownlist1"), DropDownList)
Dim rows As Data.DataRow() = ds.Tables(0).Select(expression)
Dim row As Data.DataRow
For Each row In rows
Dim newrow As Data.DataRow = mytable.NewRow()
newrow("user_id") = row("user_id")
newrow("user_name") = row("user_name")
mytable.Rows.Add(newrow)
Next row
ddl.DataSource = mytable
ddl.DataTextField = "user_name"
ddl.DataValueField = "user_id"
ddl.DataBind()
End If
End Sub

End Class

:thumbsup::D:D:D

merlykarl
06-11-2008, 09:37 AM
My coding here caused an error of :Object reference not set to an instance of an object around ddl.DataSource = PartNoInfo

Protected Sub gvPartsOrdering_rowdatabound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvPartsOrdering.RowDataBound
Dim mytable As New Data.DataTable()

If e.Row.RowType = DataControlRowType.DataRow Then

Dim ddl As DropDownList = CType(e.Row.FindControl("ddlPartNo"), DropDownList)
Dim PartNoInfo As DataSet
PartNoInfo = ddlObj.SetDropDownList("RSD_SetDDLPartNo") 'calling stored procedure of RSD_SetDDLPartNo
If (PartNoInfo Is Nothing) Then
Exit Sub
End If
ddl.DataSource = PartNoInfo
ddl.DataTextField = "SEGMENT1"
ddl.DataValueField = "SEGMENT1"
ddl.DataBind()
End If
End Sub