473,761 Members | 9,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:BoundColum n DataField="Salu tation"
HeaderText="Sal utation"></asp:BoundColumn >
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Last Name" HeaderText="Las t
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Post Code" SortExpression= "Post Code"
HeaderText="Pos t Code"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Use rs">
<ItemTemplate >
<asp:DropDownLi st id=DDlUsers runat="server" DataSource="<%#
Itemtypes %>" >
</asp:DropDownLis t>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:ButtonColu mn Text="Select" ButtonType="Pus hButton"
CommandName="Se lect"></asp:ButtonColum n>
</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_Selecte dIndexChanged(B yVal sender As Object, ByVal e
As System.EventArg s) Handles LeadsDG.Selecte dIndexChanged
Dim ddstr As String
ddstr = LeadsDG.Selecte dItem.Cells(4). Controls(0).Cli entID
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.F indControl(ddst r),
DropDownList)
TextBox1.Text = DDlist1.Selecte dItem.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 1710
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.EventArg s) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataS ource = CreateDataSourc e()
DataGrid1.DataB ind()
End If
End Sub

Private Sub DataGrid1_ItemC ommand _
(ByVal source As Object, ByVal _
e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) _
Handles DataGrid1.ItemC ommand
If e.CommandName = "Select" Then
Dim ddl As DropDownList
ddl = e.Item.FindCont rol("DDlUsers")
If Not IsNothing(ddl) Then
TextBox1.Text = "Selected " & _
ddl.SelectedIte m.Text & _
" from index # " & _
e.Item.ItemInde x.ToString
End If
End If
End Sub
Function CreateDataSourc e() 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 'CreateDataSour ce
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Last Name" HeaderText="Las t
Name"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Use rs">
<ItemTemplate >
<asp:DropDownLi st id="DDlUsers" runat="server">
<asp:ListItem Value="Mr.">Mr. </asp:ListItem>
<asp:ListItem Value="Mrs.">Mr s.</asp:ListItem>
<asp:ListItem Value="Dr.">Dr. </asp:ListItem>
<asp:ListItem Value="Ms">Ms</asp:ListItem>
</asp:DropDownLis t>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:ButtonColu mn Text="Select" ButtonType="Pus hButton"
CommandName="Se lect"></asp:ButtonColum n>
</Columns>
</asp:DataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"> </asp:TextBox></P>
"m3ckon" <an*******@devd ex.com> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...


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:BoundColum n DataField="Salu tation"
HeaderText="Sal utation"></asp:BoundColumn >
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Last Name" HeaderText="Las t
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Post Code" SortExpression= "Post Code"
HeaderText="Pos t Code"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Use rs">
<ItemTemplate >
<asp:DropDownLi st id=DDlUsers runat="server" DataSource="<%#
Itemtypes %>" >
</asp:DropDownLis t>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:ButtonColu mn Text="Select" ButtonType="Pus hButton"
CommandName="Se lect"></asp:ButtonColum n>
</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_Selecte dIndexChanged(B yVal sender As Object, ByVal e
As System.EventArg s) Handles LeadsDG.Selecte dIndexChanged
Dim ddstr As String
ddstr = LeadsDG.Selecte dItem.Cells(4). Controls(0).Cli entID
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.F indControl(ddst r),
DropDownList)
TextBox1.Text = DDlist1.Selecte dItem.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
2805
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 Description"></asp:BoundColumn> <asp:TemplateColumn runat="server" HeaderText="Id Type Option" "> <itemtemplate> <asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' /> <asp:label runat="server" ID="LlbTypeOption" Visible=False...
2
17031
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 the columns of the data grid contains a DropDownlist. I managed to create this datagrid control as follows.
2
2394
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 populate the second dropdown, as its not bale to find the specified control, even though the dropdownlist with the specified name exists in the datagrid. my .aspx page code goes like this
10
5324
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 certain control by id, but I want to find all controls of a certain type (DropDownList in this case). Is there an easier way than to get a control count of the page, loop through all controls on that page, examine their type and, if they're a...
15
3129
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 OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
1
4659
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 ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by pressing Remove button the selecetd row will be removed. I used viewstate to keep my value for postback, I want by changing selectedvalue of...
0
3500
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- 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 ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by...
1
4940
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 time. The problem is that when an item has been removed from the lookup table, and a user wants to retrieve a record that used the deleted item, the following error occurs: 'ddlAssignedTo' has a SelectedValue which is invalid because it...
0
1941
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 almost everything. but it whas made in Visual Studio 7 and i converted it to 2005. in version 7 it works almost perfect but in 2005 i am having some errors. becase all the code i am using is very long i am trying to post the nesecery code only if...
0
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.