Как удалить строку из GridView, не затрагивая базу данных - PullRequest
0 голосов
/ 08 ноября 2018

Я заполняю Gridview, используя форму с помощью AJAX. Я хочу добавить кнопку удаления в каждую строку, и при нажатии кнопки удаления эта строка должна быть удалена из 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;
                }


            }
        }
    }

Прямо сейчас я использовал кнопку Scrap / Undo, которую я хочу заменить на кнопку Delete. Также, когда я удаляю строку, это должно повлиять на количество. (т. е. утвержденные личные дни, утвержденные другие дни и утвержденные общие дни)

Изображение показывает, что когда я набираю имя и нажимаю кнопку поиска, получается GridView. Мне нужно иметь возможность удалять строки из этого вида сетки.

GridView

...