473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dropdownlist in a dtagrid



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
1 1691
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
2
by: Shiju Poyilil | last post by:
Hello, I have a datagrid with only one row and its having 2 dropdownlists, I need to populate the secodn dropdownlist on the basis of the selection in the first dropdown. but I am not able to...
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
15
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
by: asmx126453 | last post by:
Hey mensen I am having some big troubles here i tryd solving it myself with internet for 2 days but i kind fix it. Its about this i have a DotNet project that alrydi is online and working for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.