Solved

GridView and SelectedItem in DropDownList when editing

Posted on 2006-11-01
5
4,177 Views
Last Modified: 2012-06-27
I've tried for 2 days now and searched the net with no results. (well, lots of results, but still having problems)

Grid has templated fields for Item and Edit.  The Item field is a label with initials, and the Edit field is a DropDownList (that is populated from a sql data source and works just fine).  The problem when entering edit mode, I need to have the drop down show the selected initials.  currently all i can get is the first entry in the drop down list.  Any idea of what I'm missing????

  <asp:SqlDataSource ID="DropDownSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:TaxConnectionString %>"
            SelectCommand="Proc_Tax706Logs_GetAdmins" SelectCommandType="StoredProcedure">
  </asp:SqlDataSource>
 <asp:GridView
            ID="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataSourceID="TaxLogSelectStatement"
            DataKeyNames="ID, AdminInitials"
            AllowSorting="True"
            AllowPaging="True">
 <ItemTemplate>
      <table cellpadding="0" cellspacing="0" border="1" align="left" >
            <tr>
                 <td ><asp:Label ID="AccountNoLabel" STYLE="text-align:center; " runat="server"
                            Text='<%# Eval("AccountNumber") %>' foreColor="teal"  Font-Bold="True" Font-Size="12px"  Width=75 /></td>
                <td ><asp:Label ID="AccountNameLabel" runat="server"  Text='<%# Eval("AccountName") %>'
                             foreColor="teal" Font-Bold="True" Font-Size="12px"  Width="200" /></td>
                <td ><asp:Label ID="AdminLabel" runat="server" STYLE="text-align: center; " 
                            Text='<%# Eval("AdminInitials") %>&nbsp;' Font-Bold="false" Font-Size="12px" width="50" /></td>
            </tr>

       </table>
  </ItemTemplate>
  <EditItemTemplate>
       <table >
          <tr >
                <td ><asp:Label ID="Label5" runat="server" Text='Account Number' /></td>
                <td ><asp:textbox ID="AccountNoTextBox"  runat="server"
                           Text='<%# Bind("AccountNumber") %>'  /></td>
           </tr >
           <tr >
                 <td ><asp:Label ID="Label6" runat="server" Text='Name' /></td>
                 <td ><asp:textbox ID="AccountNameTextBox" AutoPostBack="false" Text='<%# Bind("AccountName") %>'  /></td>
          </tr>
          <tr id="AdministratorEditRow" >
               <td ><asp:Label ID="Label7" runat="server" style=" text-align:right;" Text='Administrator' /></td>
               <td ><asp:DropDownList ID="OfficerDropDownListUpdate" AutoPostBack="false" runat="server" DataSourceID="DropDownSqlDataSource"
                          DataTextField="AdminInitials" SelectedText='<%# Eval("AdminInitials") %>' DataValueField="AdminID" /></td>
                            </tr>
              </tr>
      </table>
  </EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

thanks for taking a look

0
Comment
Question by:PTBNA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Expert Comment

by:LeeHenry
ID: 17852169
In your code behind, you should set the dropdownlist selected value when in edit mode

OfficerDropDownListUpdate.SelectedIndex = yourSelectedIndex;

0
 

Author Comment

by:PTBNA
ID: 17852710
Ok, but which of the events should I use,
row databound,
row editing
row created,

I've worked with all of them, but am missing something to put it all together.

Do you have some sample code??

Plus, I know you can't just set the OfficerDropDownListUpdate.SelectedIndex, it has to be found in the Gridview control list.
0
 

Author Comment

by:PTBNA
ID: 17853616
Well, in case anyone's looking, I did a test, putting a label just below the Drop down list, and "Eval'd" the AdminInitials.  

And as expected they are there.  So, why with the Dropdown bound to a sql data source, and the Selected text set to the item in the grid, don't we get the Dropdownlist selected Item set???

0
 

Author Comment

by:PTBNA
ID: 17853667
Found the solution.

Cannot set the SelectedText value in the drop down.  Must use the SelectedValue.  Changing the statement, now the dropdownlist shows the selected item when we are in edit mode.

thanks for the help
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 17854794
Question closed - 500 points refunded.

Best regards,
RomMod
Experts Exchange
Community Support Moderator
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question