?
Solved

GridView and SelectedItem in DropDownList when editing

Posted on 2006-11-01
5
Medium Priority
?
4,178 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

771 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