Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Adding dropdownlist in gridview in asp.net using C#.

My source page code as follows;

ASP.NET
<asp:GridView ID="gvfaculty" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
                    BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" Height="124px"
                    Width="91px" AutoGenerateEditButton="True" AutoGenerateColumns="False">
                    <alternatingrowstyle backcolor="PaleGoldenrod" />
                    <footerstyle backcolor="Tan" />
                    <HeaderStyle BackColor="Tan" Font-Bold="True" />
                    <pagerstyle backcolor="PaleGoldenrod" forecolor="DarkSlateBlue" horizontalalign="Center" />
                    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                    <sortedascendingcellstyle backcolor="#FAFAE7" />
                    <sortedascendingheaderstyle backcolor="#DAC09E" />
                    <sorteddescendingcellstyle backcolor="#E1DB9C" />
                    <sorteddescendingheaderstyle backcolor="#C2A47B" />

                    <columns>
                    <asp:TemplateField HeaderText ="Faculty Name">
                    
                    <asp:TemplateField HeaderText = "1">
                    <itemtemplate>
                    <asp:DropDownList id="Rating1" runat="server">
                    <asp:ListItem Value="4">Excellent
                    <asp:ListItem Value="1">Good
                    <asp:ListItem Value="2">Fair
                    <asp:ListItem Value="3">Poor
                    
                    </itemtemplate>
                    

                    <asp:TemplateField HeaderText = "2">
                    <itemtemplate>
                    <asp:DropDownList id="Rating2" runat="server">
                    <asp:ListItem Value="4">Excellent
                    <asp:ListItem Value="1">Good
                    <asp:ListItem Value="2">Fair
                    <asp:ListItem Value="3">Poor
                    
                    </itemtemplate>
                    

                    <asp:TemplateField HeaderText = "3">
                    <itemtemplate>
                    <asp:DropDownList id="Rating3" runat="server">
                    <asp:ListItem Value="4">Excellent
                    <asp:ListItem Value="1">Good
                    <asp:ListItem Value="2">Fair
                    <asp:ListItem Value="3">Poor
                    
                    </itemtemplate>
                    

                    <asp:TemplateField HeaderText = "4">
                    <itemtemplate>
                    <asp:DropDownList id="Rating4" runat="server">
                    <asp:ListItem Value="4">Excellent
                    <asp:ListItem Value="1">Good
                    <asp:ListItem Value="2">Fair
                    <asp:ListItem Value="3">Poor
                    
                    </itemtemplate>
                    

                    <asp:TemplateField HeaderText = "5">
                    <itemtemplate>
                    <asp:DropDownList id="Rating5" runat="server">
                    <asp:ListItem Value="4">Excellent
                    <asp:ListItem Value="1">Good
                    <asp:ListItem Value="2">Fair
                    <asp:ListItem Value="3">Poor
                    
                    </itemtemplate>
                    
                    </columns>




In run mode output as follows;

Faculty Name      1              2             3               4           5
  
     Ram          dropdownlist1 dropdownlist2   dropdownlist3 dropdownlist4  dropdownlist5


i want to add the dropdownlist for next row in gridview.

i want the output as follows;

Faculty Name      1              2             3               4           5
  
     Ram          dropdownlist1 dropdownlist2   dropdownlist3 dropdownlist4  dropdownlist5
     Sam          dropdownlist1 dropdownlist2   dropdownlist3 dropdownlist4  dropdownlist5



for that how can i do in asp.net using c#.
Posted
v2
Comments
Raajkumar.b 19-Dec-13 0:08am    
try like this

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="3" Width="829px"

style="margin-top: 15px"
BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Height="223px"

onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound" >
<columns> <asp:TemplateField>
<HeaderTemplate>TeacherName</HeaderTemplate>
<itemtemplate><asp:Label ID="lbid" runat="server" Text='<%#Eval("TeacherName")%>'>

<asp:TemplateField>
<HeaderTemplate>1</HeaderTemplate>
<itemtemplate>
<asp:dropdownlist ID="sub1" runat="server" Width="150" >
<asp:ListItem Value="4">Excellent
<asp:ListItem Value="1">Good
<asp:ListItem Value="2">Fair
<asp:ListItem Value="3">Poor
</listitem>

<asp:TemplateField>
<HeaderTemplate>2</HeaderTemplate>
<itemtemplate>
<asp:dropdownlist ID="sub2" runat="server" Width="150" O >

<asp:TemplateField>
<HeaderTemplate>3</HeaderTemplate>
<itemtemplate>
<asp:dropdownlist ID="sub3" runat="server" Width="150" OnSelectedIndexChanged="dr_index" >

<asp:TemplateField>
<HeaderTemplate>4</HeaderTemplate>
<itemtemplate>
<asp:dropdownlist ID="sub4" runat="server" Width="150" OnDataBinding="drp_bind" OnSelectedIndexChanged="dr_index" >

What DataSource you are using? Where do you get the Ram and Sam?

If your DataSource contains two rows, then two rows will show on GridView and all other DropDownLists in the Row will also appear.

That means GridView rows are just a replica of the rows present in your DataSource.
 
Share this answer
 
v2
You can set your dropdowns in an itemtemplate like this

ASP.NET
<asp:gridview id="gv1" runat="server" xmlns:asp="#unknown">
  <columns>
    <asp:templatefield headertext="Column 1">
       <itemtemplate>
          <asp:dropdownlist id="ddl1" runat="server">
             <asp:listitem value="4" text="Excellent"></asp:listitem>
             <asp:listitem value="1" text="Good"></asp:listitem>
             <asp:listitem value="2" text="Fair"></asp:listitem>
             <asp:listitem value="3" text="Poor"></asp:listitem>
          </asp:dropdownlist>
       </itemtemplate>
    </asp:templatefield>
    <asp:templatefield headertext="Column 2">
       <itemtemplate>
          <asp:dropdownlist id="ddl2" runat="server">
             <asp:listitem value="4" text="Excellent"></asp:listitem>
             <asp:listitem value="1" text="Good"></asp:listitem>
             <asp:listitem value="2" text="Fair"></asp:listitem>
             <asp:listitem value="3" text="Poor"></asp:listitem>
          </asp:dropdownlist>
       </itemtemplate>
    </asp:templatefield>
  </columns>
</asp:gridview>
 
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