views:

237

answers:

2

I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.

I tried to use AppendDataBoundItems=true but it is still not working. I also want to set the its default value to the value that was being displayed in label of itemtemplate i.e. DropDownList's SelectedValue='<%# Eval("DepartmentName") %>' but thie property is not available to me in dropdownlist. What could be the reason. ??

<EditItemTemplate>
    <asp:DropDownList ID="ddlDepartment_Edit" runat="server" 
        DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName" 
        DataValueField="PK_DepartmentId">
    </asp:DropDownList>
    <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"  
        ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" 
        SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>                                 
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
    </asp:Label>
</ItemTemplate>   

I am using GridView

A: 

DataValueField seems to be wrong - shouldn't it be "DepartmentId"? Similarly, you need to have SelectedValue='<%# Eval("DepartmentId") %>' - DepartmentName would be the SeletectText.

VinayC
@VinayC: this is where i am in trouble. No such is property is getting visible to me i.e. SelectedValue or SelectedText, so that I can assign some value to them. About DataValueField, yes you are right it should be primary key. But not a part of discussion until these two properties gets available to me.
Shantanu Gupta
VS Designer may not be showing you the property in intelli-sense but its there. There are two properties - SelectedValue and SelectedIndex. What happens if you write SelectedIndex='<%# Eval('DepartmentId') %>'?
VinayC
My mistake - it should have been as SelectedValue = '<%# Eval('DepartmentId') %>'.
VinayC
A: 

On your grid there is an event called ItemCommand. Create a method for it:

protected void Grid1_ItemCommand(object source, GridCommandEventArgs e)

Now simply create a case statement that will recognize when the user has clicked the edit button on the grid:

case Grid.EditCommandName:
//set a member variable to the string of the cell you are editing. //something like: mString = e.item..["Column"].toString();
break;

Now you have a member variable set to the string you want to be selected before the dropdown is even loaded/prerendered. Use the event 'OnPrerender' or ' OnLoad' for the dropdownbox and set the selected item to this String.

Good Luck!

digitalsubdivide