如何从GridView中删除一行而不影响数据库

时间:2018-11-07 22:14:25

标签: c# asp.net gridview web-applications code-behind

我正在借助AJAX使用表格填充Gridview。 我想为每行添加一个删除按钮,然后单击“删除”按钮,该行应从GridView中删除(不隐藏)。

这是我的aspx代码:

<asp:Content ID="Content2" ContentPlaceHolderID="Nav" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <tr>
            <td class="auto-style1">
                <asp:Label ID="LabelFirstName" runat="server" Text="First Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBoxFirstName" runat="server" CssClass="auto-style2"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style1">
                <asp:Label ID="LabelLastName" runat="server" Text="Last Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBoxLastName" runat="server"></asp:TextBox>
            </td>

        </tr>
    </table>
    <br /><br />
    <asp:Button ID="ButtonSearch" runat="server" Text="Search" CssClass="auto-style3" Width="93px" OnClick="ButtonSearch_Click" />

    <br /><br />
    <asp:Label ID="LabelNoRows" runat="server" Text="Sorry, we couldn't find any data with this Name." Visible="false" ForeColor="Red"></asp:Label>
    <asp:Panel ID="PanelAbsenceInfo" runat="server" Visible="false">

    <table>
        <tr>
            <td class="auto-style4">
                <asp:Label ID="LabelFname" runat="server" Text="First Name:"></asp:Label>
            </td>
            <td>
                <asp:Label ID="LabelGetFirstName" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="auto-style4">
                <asp:Label ID="LabelLname" runat="server" Text="Last Name"></asp:Label>
            </td>
            <td>
                <asp:Label ID="LabelGetLname" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="auto-style4">
                <asp:Label ID="LabelEmail" runat="server" Text="Email"></asp:Label>
            </td>
            <td>
                <asp:Label ID="LabelGetEmail" runat="server"></asp:Label>
            </td>
        </tr>
         <asp:UpdatePanel ID="UpdatePanelInfo" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
         <tr>
            <td class="auto-style4">
                <asp:Label ID="LabelPersonal" runat="server" Text="Personal Days Approved" ></asp:Label>
            </td>
            <td>
                <asp:Label ID="LabelGetPersonal" runat="server" ></asp:Label>
            </td>
        </tr>
         <tr>
            <td class="auto-style4">
                <asp:Label ID="LabelOther" runat="server" Text="Other Days Approved"></asp:Label>
            </td>
            <td>
                <asp:Label ID="LabelGetOther" runat="server" ></asp:Label>
            </td>
        </tr>
         <tr>
            <td class="auto-style4">
                <asp:Label ID="LabelTotalDays" runat="server" Text="Total Days Approved" ></asp:Label>
            </td>
            <td>
                <asp:Label ID="LabelgetTotaldays" runat="server" ></asp:Label>
            </td>
        </tr>
    </table>
    <br /><br />

 <asp:GridView ID="GridViewViewAllRequests" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
      OnRowCommand="GridViewViewAllRequests_RowCommand">
        <Columns>
            <asp:BoundField DataField="scrap" Visible ="false" />
             <asp:BoundField DataField="isApproved" Visible="false"  />
            <asp:BoundField HeaderText="Date of Absence" DataField="requestedDate" DataFormatString="{0:MM/dd/yyyy}" />
            <asp:BoundField HeaderText="Rotation Period" DataField="rotationPeriod" />
            <asp:BoundField HeaderText="Reason" DataField="reason" />
            <asp:BoundField HeaderText="Days Missed" DataField="daysMissed" />
            <asp:BoundField HeaderText="Department" DataField="departmentName" />
            <asp:BoundField HeaderText="Course" DataField="courseName" />

            <asp:TemplateField HeaderText="Approved/Declined">
                <ItemTemplate>
                    <asp:Label ID="LabelApproveorDecline" runat="server" Text='<%# Eval("isApproved") == null ? "Decision not yet made." : ((bool)Eval("isApproved") ? "Approved" : "Declined") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Scrap/Undo">
                <ItemTemplate>
                    <asp:Button ID="ScrapButton" CommandArgument='<%# Eval("id") %>' runat="server" Text="Scrap" CommandName="Scrap" Visible='<%# !(bool)Eval("scrap") %>' />
                    <asp:Button ID="UndoButton" CommandArgument='<%# Eval("id") %>' runat="server" Text="Undo" CommandName="Undo"  Visible='<%# (bool)Eval("scrap") %>' />
                </ItemTemplate>
            </asp:TemplateField>


        </Columns>
    </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>



    </asp:Panel>
    <br /><br />
</asp:Content>

这是我的CS代码:

public partial class AbsenceRequestMonitor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonSearch_Click(object sender, EventArgs e)
        {
            DoAction();
        }

        public Boolean ScraptheRequest(int id, bool action)
        {
            bool success = false;
            success = DBOperation.ScrapAbsencedate(id, action);
            return success;
        }

        protected void GridViewViewAllRequests_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Determine the RowIndex of the Row whose Button was clicked.
            int id = Convert.ToInt32(e.CommandArgument);

            //Get the value of column from the DataKeys using the RowIndex.
           // int id = Convert.ToInt32(GridViewViewAllRequests.DataKeys[rowIndex].Values[0]);
           if(e.CommandName == "Scrap")
            {
                ScraptheRequest(id, true);
            }
           else if(e.CommandName == "Undo")
            {
                ScraptheRequest(id, false);
            }
            DoAction();
            UpdatePanelInfo.Update();
        }


        public void DoAction()
        {
            string firstName = null;
            string lastName = null;
            firstName = TextBoxFirstName.Text.ToString();
            lastName = TextBoxLastName.Text.ToString();
            double PdCount = 0;
            double OtherCount = 0;

            if (firstName != null && lastName != null)
            {
                //gets student data from the Student Form
                List<AbsenceMonitorData> l_studentAbsenceInfo = DBOperation.getStudentAbsenceInfo(firstName, lastName);
                AbsenceMonitorData studentAbsenceInfo = l_studentAbsenceInfo.FirstOrDefault();

                PdCount += l_studentAbsenceInfo.Where(x => x.reason == "Personal Day").Where(y => y.isApproved == true).Where(z => z.scrap == false).Select(a => a.daysMissed).Sum();
                OtherCount += l_studentAbsenceInfo.Where(x => x.reason != "Personal Day").Where(y => y.isApproved == true).Where(z => z.scrap == false).Select(a => a.daysMissed).Sum();



                GridViewViewAllRequests.DataSource = l_studentAbsenceInfo;
                GridViewViewAllRequests.DataBind();

                if (l_studentAbsenceInfo.Count > 0)
                {
                    LabelGetPersonal.Text = PdCount.ToString();
                    LabelGetOther.Text = OtherCount.ToString();
                    LabelgetTotaldays.Text = (PdCount + OtherCount).ToString();

                    LabelGetFirstName.Text = studentAbsenceInfo.firstName.ToString();
                    LabelGetLname.Text = studentAbsenceInfo.lastname.ToString();
                    LabelGetEmail.Text = studentAbsenceInfo.studentEmail.ToString();

                    PanelAbsenceInfo.Visible = true;
                    LabelNoRows.Visible = false;

                }
                else
                {
                    LabelNoRows.Visible = true;
                }


            }
        }
    }

现在我正在使用“废料/撤消”按钮,我想用“删除”按钮替换它。 另外,当我删除行时,计数应受到影响。 (即个人天数已批准,其他天数已批准和总天数已批准)

图像显示,当我键入名称并单击搜索时,将获得GridView。 我需要能够从该gridview中删除行。

GridView

0 个答案:

没有答案