Solved

GridView and SelectedItem in DropDownList when editing

Posted on 2006-11-01
5
4,172 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
  • 3
5 Comments
 
LVL 6

Expert Comment

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

OfficerDropDownListUpdate.SelectedIndex = yourSelectedIndex;

0
 

Author Comment

by:PTBNA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Question closed - 500 points refunded.

Best regards,
RomMod
Experts Exchange
Community Support Moderator
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now