473,803 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

set dropdownlist selectedindex in datagrid

Hi all,

I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.

I have a templatecolumn as follows:

<asp:TemplateCo lumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate >
<asp:DropDownLi st id="DropDownLis t3" runat="server"
AutoPostBack="T rue" OnSelectedIndex Changed="ddlsel ect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownLis t>
</ItemTemplate>

with an edit column as:
<asp:EditComman dColumn ButtonType="Lin kButton" UpdateText="Upd ate"
HeaderText="Edi t" CancelText="Can cel" EditText="Edit" ></
asp:EditCommand Column>

I would like to set the dropdownlist to the value found in the
database. my code behind uses ItemDataBound as follows:

public void myDataGrid_Item DataBound(objec t sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
DropDownList dd;
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
dd = (DropDownList)e .Item.Cells[7].FindControl("D ropDownList3");
dd.SelectedInde x =
dd.Items.IndexO f(dd.Items.Find ByText(e.Item.C ells[3].Text));
}
}

upon loading the dropdownlist has the values as in the database.
However, when I press edit the selected index becomes 0. I cannot
figure how to set the value correctly.

my edit code looks like:
public void myDataGrid_Edit Command(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
DropDownList dd;
dd = (DropDownList)e .Item.Cells[7].FindControl("D ropDownList3");
dd.SelectedInde x =
dd.Items.IndexO f(dd.Items.Find ByText(e.Item.C ells[3].Text));
<--- dd.SelectedInde x looks fine here --->
....
}

dd.SelectedInde x looks ok in debug mode, but something changes it
before the dropdownlist is displayed. the values in the dropdownlists
not being edited are ok, it's only the one being edited that shows the
wrong selectedindex.

any help would be appreciated. thanks.

Feb 25 '07 #1
1 8853
pl************* **@yahoo.com wrote:
Hi all,

I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.

I have a templatecolumn as follows:

<asp:TemplateCo lumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate >
<asp:DropDownLi st id="DropDownLis t3" runat="server"
AutoPostBack="T rue" OnSelectedIndex Changed="ddlsel ect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownLis t>
</ItemTemplate>
There is a much easier way (at least in ASP.NET 2.0)

Set the property SelectedValue on the DrowDownlist
declaratively like this:

SelectedValue=' <%# Bind("myDBField ") %>'

This will take care of everything: showing the data and
updating the data after postback.

--

Riki
Feb 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2238
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. I get the following message for this code but I suspect that the " UltraWebGrid1.Rows.Cells.Value = ddlGeography;" is not how you bind the dropdownlist control to the grid. ERROR: "The type 'System.Web.UI.WebControls.DropDownList' must be...
2
3916
by: jm | last post by:
I have datagrid. On this datagrid there is a button and a dropdownlist. When I press the button, I want the selectedIndex of the dropdownlist. There is a button and a dropdownlist for each datagrid row that is populated from the database. Example, five rows returned, five buttons, and five dropdownlists. I know I am supposed to use the ClientID, but I cannot figure out how
1
2668
by: George Durzi | last post by:
When my datagrid is in edit mode, one of my columns is edited using a drop down list. I'm able to bind the DropDownList to a DataSource when in edit mode. HOWEVER, I can't preset the DropDownList's value when the Edit button is clicked for that row inside the datagrid. Here's a simplified version of my datagrid: <asp:datagrid id="dgHPU" runat="server"
3
4379
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would be really grateful for any assistance! Thanks
2
2347
by: Kay Chan | last post by:
Hi All, I am going to using a editable datagrid, one of the field should be a dropdownlist, here is my code <EditItemTemplate> <asp:DropDownList ID="INVESTMENT_FUND_ID" DataSource="<%# getINVESTMENT_FUND() %>" DataTextField="NAME" DataValueField="VALUE"
4
2013
by: Mark Waser | last post by:
I've discovered a very odd bug when attempting to put a dropdown list in a datagrid. In the page PreRender step, the selected index of the datagrid is successfully set during databinding. Yet, when the datagrid enters it's own OnPreRender, the selected index has reverted to zero. I created a debug version of the dropdown list which inherited from dropdownlist and overrode the selected index property to trace.write whenever it was...
4
1735
by: glenn | last post by:
Hi folks, I am getting an error "Object reference not set to an instance of an object". It seems I have everything in place but something is obviously in err. If you could take a quick peak at my code segments and provide any insight in a reply, I would be eternally grateful. My table name is "rfi" and the field with a dropdownlist control is called "contract". I have imported the System.Data and System.Data.OleDb libraries. ...
2
16101
by: glenn | last post by:
Hi folks, I am trying to determine which item in a DropDownList Web control has been selected. I have posted an OnSelectedIndexChanged subroutine in my code with a reference to the subroutine in my asp tag as shown here: <asp:DropDownList id="ddlTo" runat="server"
1
3228
by: marcbb | last post by:
Hi all, I have a really strange problem working with Dropdownlists in a DataGrid. I'm trying to preselect some values from the DropDownlist for each row in the DataGrid, but it seems that both selectedIndex has the same reference and modifying the first one also automatically modify the second one. For example:
0
9703
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
10550
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...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
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
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4275
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
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.