【问题标题】:Delete Individual row of gridview without deleting from database删除gridview的单个行而不从数据库中删除
【发布时间】:2014-05-15 17:38:18
【问题描述】:

我在 ASP.NET 和 C# 中使用 Linq-to-SQL。我想通过单击带有gridTrainingNeed_RowDeleting 事件的删除按钮(即CommandField 或TemplateField)来删除gridview 的单个行。

这是我在页面加载时加载 gridview 的代码。

int departmentId;
int branchId;
int positionId;
double month;
int levelId;

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            Initialize();
        }
}

public void Initialize()
{
        ddlBranch.DataSource = TrainingManager.GetBranchName();
        ddlBranch.DataBind();

        ddlDepartment.DataSource = DepartmentManager.GetAllDepartments();
        ddlDepartment.DataBind();

        ddlPosition.DataSource = TrainingManager.GetDesignationName();
        ddlPosition.DataBind();

        ddlLevel.DataSource = TrainingManager.GetLevelName();
        ddlLevel.DataBind();

        gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
        gridTrainingNeed.DataBind();
}

现在我将保留我的设计代码:

 <div class="innerLR">
<div class="seperator bottom" />
    <div class="widget">
<div class="widget-head">
<h4 class="heading">
Send Circulation To
</h4>
</div>
<div class="widget-body">
    <table class="tableShow" cellpadding="3" cellspacing="0">
        <tr>
            <td>
                <asp:DropDownList ID="ddlBranch" runat="server" DataTextField="Name" 
                    DataValueField="BranchId" Width="170px" AppendDataBoundItems="True" 
                    >
                    <asp:ListItem Text="---Select Branch---"/>
                </asp:DropDownList>
               </td>
            <td>
                <asp:DropDownList ID="ddlDepartment" runat="server" AppendDataBoundItems="True" 
                    DataTextField="Name" Width="170px" DataValueField="DepartmentId">
                    <asp:ListItem Text="---Select Department---"/>
                </asp:DropDownList></td>
            <td>
                <asp:DropDownList ID="ddlLevel" runat="server" DataTextField="Name" 
                    DataValueField="LevelId" Width="170px" AppendDataBoundItems="True" 
                    >
                    <asp:ListItem Text="---Select Level---"/>
                </asp:DropDownList></td>
            <td>
                <asp:DropDownList ID="ddlPosition" runat="server" DataTextField="Name" 
                    DataValueField="DesignationId" Width="170px" AppendDataBoundItems="True" 
                    >
                    <asp:ListItem Text="---Select Position---"/>
                </asp:DropDownList></td>
        </tr>
        <tr>
            <td>

                <br />

                Send Circulation To</td>
            <td>
                <br />
                Month</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:DropDownList ID="ddlCirculation" runat="server" DataTextField="Name" 
                    DataValueField="PositionId" Width="170px" AutoPostBack="true" AppendDataBoundItems="True" 
                    onselectedindexchanged="ddlCirculation_SelectedIndexChanged">
                <asp:ListItem Text="" Value="-1" />
                <asp:ListItem Text="All" Value="0" />
                <asp:ListItem Text="Must Have Worked For" Value="1" />
                </asp:DropDownList></td>
            <td>
                <asp:TextBox ID="txtMonth" Width="150px" runat="server" Enabled="false"></asp:TextBox></td>
            <td>
                <asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" /></td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </div>
    </div>




<div>
    <asp:GridView ID="gridTrainingNeed" runat="server" AutoGenerateColumns="False" 
        EnableModelValidation="True" CellPadding="4" ForeColor="#333333" 
        GridLines="None" >
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="EmployeeId" HeaderText="EID" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Branch" HeaderText="Branch" />
            <asp:BoundField DataField="Department" HeaderText="Department" />
            <asp:BoundField DataField="Level" HeaderText="Level" />
            <asp:BoundField DataField="Position" HeaderText="Position" />
            <asp:CommandField HeaderText="Delete" ShowCancelButton="False" 
                ShowDeleteButton="True" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    </asp:GridView>

</div>
</div>

因此,我想使用 RowDeleting 事件逐行删除行,以便在 gridview 上显示数据而不是来自数据库。

 protected void gridTrainingNeed_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {//Row delete code
   }

谢谢。

【问题讨论】:

    标签: c# asp.net sql linq gridview


    【解决方案1】:

    而不是像你正在做的那样直接分配DataSource:

    gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
    

    将结果存储在页面级列表中。定义一个页面级列表,如:

    //Define List at page/class level
    List<Employee> gridList = new List<Employee>();
    

    然后在你的方法Initialize

    gridList = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
    gridTrainingNeed.DataSource = gridList;
    gridTrainingNeed.DataBind();
    

    您需要在 PostBack 上将 List&lt;Employee&gt; 持久保存在内存中,使用 Session/ViewState 等(请参阅 ASP.Net State Management)。

    稍后在您的gridTrainingNeed_RowDeleting 中,从行或唯一标识员工的任何字段中获取ID。查询您的列表并从内存中的列表中删除该对象。将 DataSource 重新分配给修改后的列表并调用 DataBind。

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多