Click here to Skip to main content
15,921,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I designed a Gridview that contails 4 columns.The data is bounded code is as follows:

XML
<asp:GridView ID="GViewCarrierType" runat="server" AllowPaging="True" DataKeyNames=""
                AutoGenerateColumns="False" AutoGenerateEditButton="True" CellPadding="4" ForeColor="#333333"
                GridLines="None" PageSize="5" OnPageIndexChanging="GViewCarrierType_PageIndexChanging"
                OnRowCancelingEdit="GViewCarrierType_RowCancelingEdit" OnRowEditing="GViewCarrierType_RowEditing"
                OnRowUpdating="GViewCarrierType_RowUpdating">
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#EFF3FB" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="AliceBlue" />
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                        <HeaderTemplate>
                            Carrier Type</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval ("CarrierTypeName") %></ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtCTName" runat="server" Text='<%#Eval ("CarrierTypeName") %>'></asp:TextBox></EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtnewCTName" runat="server"></asp:TextBox></FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                        <HeaderTemplate>
                            Mode Of Transport</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval ("MTName") %></ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlMT" runat="server" AppendDataBoundItems="true"  DataSource='<%# GetMTName()%>' SelectedValue='<%#Eval("MTName")%>'  DataTextField="MTName"
                                DataValueField="MTID" >
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <FooterTemplate>
                           <asp:DropDownList ID="ddlnewMT" runat="server" DataSource='<%# GetMTName()%>' DataTextField="MTName"
                                DataValueField="MTID" ></asp:DropDownList></FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                        <HeaderTemplate>
                            IsActive</HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkstatus" runat="server" Checked='<%#Eval("CTIsActive")%>' Enabled="false" /></ItemTemplate>
                        <EditItemTemplate>
                            <asp:CheckBox ID="chkstatus" runat="server" Checked='<%#Eval("CTIsActive")%>' /></EditItemTemplate>
                        <FooterTemplate>
                            <asp:CheckBox ID="chknewstatus" runat="server" /></FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                        <FooterTemplate>
                            <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="InsertCarrierType" /></FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>


Whenever user clicked "Edit" linkbutton of Gridview that praticular row will display in edit mode in such a way that one of the column values will display in Dropdownlist.

My requirement is the default value of droupdownlist should be same value as value of that column when gridview is un editble mode.

Please any body help me to get my requirement.

Thanks
Uday
Posted

You have to use FindControl[^] to access the DropDownList. Below link may help you.

Accessing the different controls inside a GridView control[^]
 
Share this answer
 
dropdown.SelectedValue as the column in the ItemCommand if you are using ItemCommand event for editing
 
Share this answer
 
You can find it here. Code Project - Frequently Asked Questions Series 1: The ASP.Net GridView[^]
Apart from that there are many FAQ's related with GridView which are Frequently asked in this forum.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900