By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Dropdownlist in a dtagrid

P: n/a


Hi there,

please help if you can, I'm having an issue with droponnlists in a
datagrid

I have a datagrid which is populated from a query .. all works fine

I've added two extra columns, one is a dropdownlist and the other is a
button which runs the command selectcode for this:

<Columns>
<asp:BoundColumn DataField="Salutation"
HeaderText="Salutation"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Post Code" SortExpression="Post Code"
HeaderText="Post Code"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Users">
<ItemTemplate>
<asp:DropDownList id=DDlUsers runat="server" DataSource="<%#
Itemtypes %>" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>

The datagrid renders correctly and when I click the button I've written
code to find the id of the dropdownlist on the same row as the button
which has been clicked ... however I can't access the dropdownlist value
and I NEED to do this in order to continue with my project.
My code is listed below:

Private Sub LeadsDG_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles LeadsDG.SelectedIndexChanged
Dim ddstr As String
ddstr = LeadsDG.SelectedItem.Cells(4).Controls(0).ClientID
ddstr = Replace(ddstr, "__ctl1", "_DDlUsers")

End Sub
Please someone tell me how to access the drop down, I've tried this
(with writing the vlue to a text box):

Dim DDlist1 As DropDownList = CType(LeadsDG.FindControl(ddstr),
DropDownList)
TextBox1.Text = DDlist1.SelectedItem.Value

But I get the following error when I click a value:

Object reference not set to an instance of an object.

Please help me access the dropdownlist values, I know that the sub is
being run and that the id is the correct control, but I'm stuck getting
it working
Regrds,

m3ckon

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello <insert name here>,

It seems like you're doing something the hard way. All you need is a
reference to the selected row and from that you can use FindControl to
locate the ddl. With that you pull out the value. Below, I've inserted a
working sample that might get you going. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, ByVal _
e As System.Web.UI.WebControls.DataGridCommandEventArgs ) _
Handles DataGrid1.ItemCommand
If e.CommandName = "Select" Then
Dim ddl As DropDownList
ddl = e.Item.FindControl("DDlUsers")
If Not IsNothing(ddl) Then
TextBox1.Text = "Selected " & _
ddl.SelectedItem.Text & _
" from index # " & _
e.Item.ItemIndex.ToString
End If
End If
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("SS", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("LastName", GetType(String)))
dt.Columns.Add(New DataColumn _
("FirstName", GetType(String)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i + i
dr(1) = "LastName " + i.ToString()
dr(2) = "FirstName " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="FirstName" HeaderText="First
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last
Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Users">
<ItemTemplate>
<asp:DropDownList id="DDlUsers" runat="server">
<asp:ListItem Value="Mr.">Mr.</asp:ListItem>
<asp:ListItem Value="Mrs.">Mrs.</asp:ListItem>
<asp:ListItem Value="Dr.">Dr.</asp:ListItem>
<asp:ListItem Value="Ms">Ms</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
"m3ckon" <an*******@devdex.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...


Hi there,

please help if you can, I'm having an issue with droponnlists in a
datagrid

I have a datagrid which is populated from a query .. all works fine

I've added two extra columns, one is a dropdownlist and the other is a
button which runs the command selectcode for this:

<Columns>
<asp:BoundColumn DataField="Salutation"
HeaderText="Salutation"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Post Code" SortExpression="Post Code"
HeaderText="Post Code"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Users">
<ItemTemplate>
<asp:DropDownList id=DDlUsers runat="server" DataSource="<%#
Itemtypes %>" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>

The datagrid renders correctly and when I click the button I've written
code to find the id of the dropdownlist on the same row as the button
which has been clicked ... however I can't access the dropdownlist value
and I NEED to do this in order to continue with my project.
My code is listed below:

Private Sub LeadsDG_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles LeadsDG.SelectedIndexChanged
Dim ddstr As String
ddstr = LeadsDG.SelectedItem.Cells(4).Controls(0).ClientID
ddstr = Replace(ddstr, "__ctl1", "_DDlUsers")

End Sub
Please someone tell me how to access the drop down, I've tried this
(with writing the vlue to a text box):

Dim DDlist1 As DropDownList = CType(LeadsDG.FindControl(ddstr),
DropDownList)
TextBox1.Text = DDlist1.SelectedItem.Value

But I get the following error when I click a value:

Object reference not set to an instance of an object.

Please help me access the dropdownlist values, I know that the sub is
being run and that the id is the correct control, but I'm stuck getting
it working
Regrds,

m3ckon

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.