0

I have a gridview1 and it has a normal field. After clicking edit link, I want to show a DropdownList that reads from my Database. How can I achieve this?

this is my code for list template:

<asp:TemplateField HeaderText="Course Name">
     <ItemTemplate>
         <asp:Label ID="Label7" runat="server" Text='<%# Bind("CName") %>'></asp:Label>
     </ItemTemplate>

       <EditItemTemplate>

         <asp:DropDownList ID="ddlECName"  CssClass="auto-style16" Width="80px" runat="server" ></asp:DropDownList>

           </EditItemTemplate>

           <FooterTemplate>

                  <asp:DropDownList ID="ddlCName"  CssClass="auto-style16" Width="80px" runat="server" ></asp:DropDownList>

            </FooterTemplate>

 </asp:TemplateField>

for adding values in ddl :

    //This code for adding values in course name list in the edit mood

 DropDownList ddlECName = ????? as DropDownList; 

                ddlECName.DataSource = GetData("SELECT * FROM TBCourse  INNER JOIN TbCourseMajor ON  TBCourse.CId = TbCourseMajor.CId  AND TbCourseMajor.MNom='" + DLMNom.SelectedValue + "'");


                ddlECName.DataTextField = "CName";
                ddlECName.DataValueField = "CId";
                ddlECName.DataBind();

                //Add Default Item in the DropDownList
                ddlECName.Items.Insert(0, new ListItem("----"));

The question is what should I write to define ddlECName ? how to tell them that I mean the ddl that in edit mode of grid view?

The second question is where to write this in which function ? Ex: OnRowEditing...etc

I have confusion in dealing with grid view methods if you know a good explanation please give the url. thank you

Niklas
  • 955
  • 15
  • 29
AL3MS
  • 91
  • 3
  • 9

1 Answers1

1

It would go in your RowDataBound event. So you will have to check the row state (edit) and bind the dropdown.

Please refer- Gridview row editing - dynamic binding to a DropDownList https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events(v=vs.110).aspx http://www.codeproject.com/Articles/36528/GridView-all-in-one

Community
  • 1
  • 1
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78