【问题标题】:Gridview with dropdownlist from the database and outside add,edit delete buttons outside the gridview ? ASP.NET C#Gridview 与来自数据库的下拉列表和外部添加,编辑在gridview 之外的删除按钮? ASP.NET C#
【发布时间】:2017-11-02 19:16:26
【问题描述】:

您好,我需要帮助如何创建带有文本框的网格视图和 3 个具有不同表脚模板属性的下拉列表,以添加新信息和外部按钮。 我看到了这个例子,但我不想创建过程,我不知道该怎么做。

http://www.asparticles.com/2017/02/aspnet-gridview-crud-operations-with-button-controls-outside-of-gridview-control.html

    <h2>FORECAST SALE ACTIVITY</h2>
 <br /> <br /> <br />
      <table>
     <tr>
     <td>
       <asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
     </td>
     </tr>
     <tr>
     <td>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"  AutoGenerateColumns="False" 
          ShowFooter="True" AllowPaging="True" BackColor="White" BorderColor="#CC9966" 
          BorderStyle="Solid" BorderWidth="1px" CellPadding="3"  
          DataKeyNames="Activity_ID" style="text-align: left">
             <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="#001328" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#001328" ForeColor="white" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#336699" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
    <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkRow" runat="server"></asp:CheckBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Activity ID">
                 <ItemStyle Width="75px" />
<HeaderStyle  Width="75px" />
 <FooterStyle Width="75px" />
                <ItemTemplate>
                    <asp:Label ID="lblActivity" runat="server" Text='<%#Eval("Activity_ID")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtActivity" runat="server"  Width="75px" Text='<%#Eval("Activity_ID")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtActivity"  Width="75px" runat="server"> </asp:TextBox>
                </FooterTemplate>              
            </asp:TemplateField>       
            <asp:TemplateField HeaderText="Date" >
                <ItemStyle Width="65px" />
<HeaderStyle  Width="65px" />
 <FooterStyle Width="65px" /> 
                <ItemTemplate>                 
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtDate" Width="65px" runat="server" Text='<%#Eval("Date","{0:dd-MMM-yy}")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtDate"  Width="65px" runat="server" Text='<%#Eval("Date","{0:dd-MMM-yy}")%>'> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
         <asp:TemplateField HeaderText="Quatation Number">
              <ItemStyle Width="100px" />
<HeaderStyle  Width="100px" />
 <FooterStyle Width="100px" />
                <ItemTemplate>
                    <asp:Label ID="lblQno" runat="server" Text='<%#Eval("Quatation_Number")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtQno" Width="100px" runat="server" Text='<%#Eval("Quatation_Number")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtQno"  Width="100px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Customer Name">
              <ItemStyle Width="130px" />
<HeaderStyle  Width="130px" />
 <FooterStyle Width="130px" />  
                <ItemTemplate>
                    <asp:Label ID="lblCName" runat="server" Text='<%#Eval("Customer_Name")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="eddlCName" Width="130px" runat="server">
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="fddlCName" Width="130px" runat="server">
                    </asp:DropDownList>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Machine Model">
            <ItemStyle Width="105px" />
<HeaderStyle  Width="105px" />
 <FooterStyle Width="105px" />
                <ItemTemplate>
                    <asp:Label ID="lblMmodel" runat="server" Text='<%#Eval("Machine_Model")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="eddlMmodel" Width="105px" runat="server">
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="fddlMmodel" Width="105px" runat="server">
                    </asp:DropDownList>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity" >
                <ItemStyle Width="60px" />
<HeaderStyle  Width="60px" />
 <FooterStyle Width="60px" />
            <ItemTemplate>
                    <asp:Label ID="lblQuantity" runat="server" Text='<%#Eval("Quantity")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtQuantity" Width="60px" runat="server" Text='<%#Eval("Quantity")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtQuantity" Width="60px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Value GBR" >
            <ItemStyle Width="80px" />
<HeaderStyle  Width="80px" />
 <FooterStyle Width="80px" />
                <ItemTemplate>
                    <asp:Label ID="lblvalueGBR" runat="server" Text='<%#Eval("valueGBR")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtvalueGBR" Width="80px" runat="server" Text='<%#Eval("valueGBR")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtvalueGBR" Width="80px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Value EUR" >

            <ItemStyle Width="80px" />
<HeaderStyle  Width="80px" />
 <FooterStyle Width="80px" />
                <ItemTemplate>
                    <asp:Label ID="lblvalueEUR"  runat="server" Text='<%#Eval("valueEUR")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtvalueEUR" Width="80px" runat="server" Text='<%#Eval("valueEUR")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtvalueEUR" Width="80px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Rate" >           
            <ItemStyle Width="50px" />
<HeaderStyle  Width="50px" />
 <FooterStyle Width="50px" />
                <ItemTemplate>
                    <asp:Label ID="lblRate" runat="server" Text='<%#Eval("Rate")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtRate"  Width="50px" runat="server" Text='<%#Eval("Rate")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtRate" Width="50px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Weighted Value" >            
            <ItemStyle Width="110px" />
<HeaderStyle  Width="110px" />
 <FooterStyle Width="110px" />
                <ItemTemplate>
                    <asp:Label ID="lblweightedValue" Width="90px" runat="server" Text='<%#Eval("weightedValue")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtweightedValue" Width="90px" runat="server" Text='<%#Eval("weightedValue")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtweightedValue" Width="90px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>           
     <asp:TemplateField HeaderText="Status">
         <ItemStyle Width="97px" />
<HeaderStyle  Width="97px" />
 <FooterStyle Width="97px" />
                <ItemTemplate>
                    <asp:Label ID="lblStatus" runat="server" Text='<%#Eval("Status")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="eddlStatus" Width="97px" runat="server">
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="fddlStatus"  Width="97px" runat="server">
                    </asp:DropDownList>
                </FooterTemplate>
            </asp:TemplateField>        
        <asp:TemplateField HeaderText="Estimated Decision Date" >
            <ItemStyle Width="170px" />
<HeaderStyle  Width="170px" />
 <FooterStyle Width="170px" />
                <ItemTemplate>
                    <asp:Label ID="lblestDecisionDate" runat="server" Text='<%#Eval("estDecisionDate","{0:dd-MMM-yy}")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtestDecisionDate" Width="90px" runat="server" Text='<%#Eval("estDecisionDate","{0:dd-MMM-yy}")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtestDecisionDate"  Width="90px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Promised Delivery Date" >
             <ItemStyle Width="165px" />
<HeaderStyle  Width="165px" />
 <FooterStyle Width="165px" />
                <ItemTemplate>
                    <asp:Label ID="lblPromisedDeliveryDate" runat="server" Width="90px" Text='<%#Eval("PromisedDeliveryDate","{0:dd-MMM-yy}")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="etxtPromisedDeliveryDate" runat="server" Width="90px" Text='<%#Eval("PromisedDeliveryDate","{0:dd-MMM-yy}")%>'> </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="ftxtPromisedDeliveryDate"  Width="90px" runat="server"> </asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Agent Name">
             <ItemStyle Width="90px" />
<HeaderStyle  Width="90px" />
 <FooterStyle Width="90px" />
                <ItemTemplate>
                    <asp:Label ID="lblAname" runat="server" Text='<%#Eval("Agent_Name")%>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="eddlAname" Width="90px" runat="server">
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="fddlAname" Width="90px" runat="server">
                    </asp:DropDownList>
                </FooterTemplate>
            </asp:TemplateField>
    </Columns>
    </asp:GridView>
         </td>
     </tr>
     <tr>
     <td>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CobornSalesDBConnectionString %>" SelectCommand="SELECT SalesActivity.Activity_ID,SalesActivity.Date, SalesActivity.Quatation_Number, CUSTOMER.Customer_Name, PRODUCTS.Machine_Model, SalesActivity.Quantity, SalesActivity.valueGBR, SalesActivity.valueEUR, SalesActivity.Rate, SalesActivity.weightedValue, STATUS.Status, SalesActivity.estDecisionDate, SalesActivity.PromisedDeliveryDate, AGENTS.Agent_Name FROM SalesActivity INNER JOIN CUSTOMER ON SalesActivity.Customer_ID = CUSTOMER.Customer_ID INNER JOIN PRODUCTS ON SalesActivity.Product_ID = PRODUCTS.Product_ID INNER JOIN STATUS ON SalesActivity.Status_ID = STATUS.Status_ID INNER JOIN AGENTS ON SalesActivity.Agent_ID = AGENTS.Agent_ID"></asp:SqlDataSource>

    <table align="right" style="width: 531px">
         <tr style="width:500px;">
             <td>
               <asp:Button ID="btnAdd" runat="server" Text="Add" CssClass="button" OnClick="btnAdd_Click" />
           </td>
           <td>
               <asp:Button ID="btnEdit" runat="server" Text="Edit" CssClass="button" OnClick="btnEdit_Click" />
               <asp:Button ID="btnESave" runat="server" Text="Update" CssClass="button" OnClick="btnESave_Click" />
           </td>
           <td>
               <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="button" OnClick="btnDelete_Click" />
           </td>
           <td>
               <asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="button" OnClick="btnCancel_Click" />
             </td>
         </tr>
       </table>
     </td>
     </tr>
   </table>

【问题讨论】:

  • 你要对哪一列应用下拉列表,还有,你是用sqldatasource绑定的吗?
  • 您好,我需要 machine_model、customer_name、agent_name 一个下拉列表,我仍然没有从 cs 文件开始,因为我不知道该怎么做 :(。如果您看到我发布的链接,那就是我想要的成为最终结果,但创建它的人正在使用过程,我不想使用它

标签: c# asp.net gridview


【解决方案1】:

请尝试以下操作,注意这还需要您将 DropDownList 绑定到另一个 SqlDataSource,它将显示该列中的所有值:

<asp:TemplateField HeaderText="EddlA Name" SortExpression="eddlAname">
             <EditItemTemplate>
               <asp:DropDownList ID="eddlAname" runat="server" Text='<%# Bind("eddlAname") %>' CssClass="form-control" RenderMode="Lightweight" 
                 DataSourceID="SqlDataSource2" AppendDataBoundItems="true" DataTextField="eddlAname" DataValueField="eddlAname" BackColor="Transparent"></asp:DropDownList>
             </EditItemTemplate>
             <ItemTemplate>
               <asp:Label ID="LBLeddlAname" runat="server" Text='<%# Bind("eddlAname") %>' ReadOnly="true" BackColor="Transparent" ForeColor="Black"></asp:Label>
             </ItemTemplate>
           </asp:TemplateField>

然后,您的其他 SqlDataSource 将类似于,这将用于绑定 eddlAname 列。

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CobornSalesDBConnectionString %>"
 SelectCommand="SELECT DISTINCT [eddlAname] FROM [EntityNameHere]"></asp:SqlDataSource>

【讨论】:

  • 谢谢,我现在就试试,你知道我如何创建按钮 - 在 gridview 之外添加、编辑、删除
  • 我查看了您提供的链接,他们用于按钮 (C#) 的代码可以工作,只是不包含 BindGrid();方法,而是使用:GridView1.DataBind();并且您无需再次指定数据源。
  • 我没有足够的时间来解释所有的代码,但是考虑这个例子:c-sharpcorner.com/UploadFile/44fb93/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多