使用c#在asp.net的gridview中添加下拉列表 [英] adding dropdownlist in gridview in asp.net using c#

查看:271
本文介绍了使用c#在asp.net的gridview中添加下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#在asp.net的gridview中添加下拉列表。



我的源页面代码如下;



 <   asp:GridView     ID   =  gvfaculty     runat   =  server    BackColor   =  LightGoldenrodYellow    BorderColor   =  Tan  

BorderWidth = 1px CellPadding = 2 ForeColor = 黑色 GridLines = 高度 = 124px

宽度 = 91px AutoGenerateEditButton = < span class =code-keyword> True AutoGenerateColumns = 错误 >
< alternatingrow style backcolor = PaleGoldenrod / >
< ; 页脚 样式 < span class =code-attribute> backcolor = Tan / >
< HeaderStyle BackColor = Tan Font-Bold = True / >
< pager style backcolor = PaleGoldenrod forecolor = DarkSlateBlue horizo​​ntalalign = 中心 / >
< SelectedRowStyle BackColor = DarkSlateBlue ForeColor = GhostWhite / >
< sortedascendingcell style backcolor = #FAFAE7 / >
< <跨度class =code-leadattribute> sortedascendingheader style backcolor = #DAC09E / >
< ; sorteddescendingcell style < span class =code-attribute> backcolor = #E1DB9C / >
< sorteddescendingheader style backcolor = #C2A47B / >

< > ;
< asp:TemplateField HeaderText = 教师姓名 >

< asp:TemplateField HeaderText = 1 >
< itemtemplate >
< asp:DropDownList < span class =code-attribute> id = Rating1 runat = server >
< asp:ListItem Value = 4 > 优秀
< asp:ListItem = 1 >
< asp:ListItem = 2 > Fair
< asp:ListItem < span class =code-attribute> = 3 > P. oor

< / itemtemplate >


< asp:TemplateField HeaderText = 2 >
< itemtemplate >
< asp:DropDownList id = Rating2 runat = 服务器 >
< asp:ListItem = 4 > 优秀
< asp:ListItem = 1 >
< asp:ListItem = 2 > Fair
< asp:ListItem = 3 < span class =code-keyword>>


< / itemtemplate >


< asp:TemplateField HeaderText = 3 >
< itemtemplate >
< asp:DropDownList id = Rating3 runat = 服务器 >
< asp:ListItem = 4 > 优秀
< asp:ListItem = 1 >
< asp:ListItem = 2 > Fair
< asp:ListItem = 3 >

< / itemtemplate >


< asp:TemplateField HeaderText = 4 >
< itemtemplate >
< asp:DropDownList id = Rating4 < span class =code-attribute> runat = 服务器 <跨度class =code-keyword>>
< asp:ListItem = 4 > 优秀
< < span class =code-leadattribute> asp:ListItem = 1 >
< asp:ListItem = 2 > 公平
< asp:ListItem = 3 > ;

< / itemtemplate >


< asp:TemplateField HeaderText = 5 >
< itemtemplate >
< asp:DropDownList id = Rating5 runat = 服务器 >
< asp:ListItem = 4 > 优秀
< asp:ListItem = 1 >
< asp:ListItem = 2 > Fair
< asp:ListItem = 3 >

< / itemtemplate >

< / colu mns >









在运行模式下输出如下;



学院名称1 2 3 4 5 

Ram dropdownlist1 dropdownlist2 dropdownlist3 dropdownlist4 dropdownlist5





i想要为gridview中的下一行添加下拉列表。



i想要输出如下;



学院名称1 2 3 4 5 

Ram dropdownlist1 dropdownlist2 dropdownlist3 dropdownlist4 dropdownlist5
Sam dropdownlist1 dropdownlist2 dropdownlist3 dropdownlist4 dropdownlist5







为什么我在asp.net中使用c#。

解决方案

如果您的 DataSource 包含两行,则 GridView 和其他所有将显示两行也会出现行中的DropDownLists



这意味着 GridView 行是只是 DataSource 中存在的行的副本。


您可以在这样的itemtemplate中设置下拉菜单



 <   asp:gridview     id   =  gv1    runat   =  server    xmlns: asp   = #unknown >  
< >
< asp:templatefield headertext = 第1列 >
< itemtemplate >
< < span class =code-leadattribute> asp:dropdownlist id = ddl1 runat = 服务器 >
< asp:listitem value = 4 text = 优秀 > < / asp:listitem >
< asp:listitem value = 1 text = > < / asp:listitem >
< asp:listitem = 2 text = 公平 < span class =code-keyword>> < / asp:listitem < span class =code-keyword>>
< asp:listitem value = 3 text = > < / asp:listitem >
< / asp:dropdownlist >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield < span class =code-attribute> headertext = 第2列 >
< itemtemplate >
< asp:dropdownlist id = ddl2 runat = 服务器 >
< asp:listitem value = 4 text = 优秀 < span class =code-keyword>> < / asp:listitem < span class =code-keyword>>
< asp:listitem value = 1 text = > < / asp:listitem >
< asp:listitem value = 2 text = < span class =code-keyword>合格 > < / asp:listitem >
< asp:listitem value = 3 文本 = > < / asp:listitem >
< / asp:dropdownlist < span class =code-keyword>>
< / itemtemplate >
< / asp: templatefield >
< / columns >
< / asp:gridview >


Adding dropdownlist in gridview in asp.net using C#.

My source page code as follows;

<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#.

解决方案

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.


You can set your dropdowns in an itemtemplate like this

<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>


这篇关于使用c#在asp.net的gridview中添加下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆