如何在ASP.net GridView中按列删除重复项

时间:2014-05-03 05:29:20

标签: c# asp.net gridview

我的ASPX页面中有以下GridView

<asp:GridView ID="tbl_report" runat="server" AutoGenerateColumns="False" GridLines="None" CssClass="CSSTable">

        <Columns>  
        <asp:BoundField DataField="StudentID" HeaderText="Student ID" HeaderStyle-Width="300px"/> 
         <asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="250px"/>  
        <asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="250px"/> 
         <asp:BoundField DataField="ExamID" HeaderText="Exam ID" HeaderStyle-Width="250px"/> 
          <asp:BoundField DataField="ExamName" HeaderText="Exam Name" HeaderStyle-Width="250px"/> 
          <asp:BoundField DataField="SubjectName" HeaderText="Subject" HeaderStyle-Width="250px"/> 
         <asp:BoundField DataField="Marks" HeaderText="Marks" HeaderStyle-Width="100px"/> 

      <asp:BoundField DataField="Result" HeaderText="Result" HeaderStyle-Width="100px"/>  
    </Columns>  

</asp:GridView>

通过以下方法生成

protected void view_Report(object sender, EventArgs e)
        {
            String classname = txt_classname.SelectedValue;
            db = new DBConnection();
            db.getConnection();
            dt = db.executeSelectQuery("select s.StudentID,s.FirstName,s.LastName,eh.ExamID,eh.ExamName,sub.SubjectName,se.Marks,se.Result from ExamHeader eh inner join Grade g on eh.GradeName = g.GradeName inner join Class c on c.GradeName = g.GradeName inner join StudentToExam se on se.ExamID = eh.ExamID inner join Student s on s.StudentID = se.StudentID inner join Subject sub on eh.ExamSubjectID = sub.SubjectID where c.ClassName = '"+classname+"' and eh.ExamPublished = 'Yes' and eh.ExamDate <= GETDATE() order by s.StudentID");
            tbl_report.DataSource = dt;
            tbl_report.DataBind();
        }

例如,我得到以下结果

enter image description here

我想要做的是删除重复的数据,并将这3列留空,就像上面我用红色四舍五入的情况一样。请建议我一个方法。

1 个答案:

答案 0 :(得分:1)

我认为下面的逻辑会帮助你。你需要RowDataBound Event来设置重复列。

protected void view_Report(object sender, EventArgs e)
        {
    //=====Change Code===========================
        ViewState["PrevRecordStudentID"] = 0;
            ViewState["PrevRecordFirstName"] = "";
            ViewState["PrevRecordLastName"] = "";
    //=====Change Code===========================

            String classname = txt_classname.SelectedValue;
            db = new DBConnection();
            db.getConnection();
            dt = db.executeSelectQuery("select s.StudentID,s.FirstName,s.LastName,eh.ExamID,eh.ExamName,sub.SubjectName,se.Marks,se.Result from ExamHeader eh inner join Grade g on eh.GradeName = g.GradeName inner join Class c on c.GradeName = g.GradeName inner join StudentToExam se on se.ExamID = eh.ExamID inner join Student s on s.StudentID = se.StudentID inner join Subject sub on eh.ExamSubjectID = sub.SubjectID where c.ClassName = '"+classname+"' and eh.ExamPublished = 'Yes' and eh.ExamDate <= GETDATE() order by s.StudentID");
            tbl_report.DataSource = dt;
            tbl_report.DataBind();
        }


//=====Add Code===========================
 protected void tbl_report_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text == Convert.ToString(ViewState["PrevRecordStudentID"]) && e.Row.Cells[1].Text == Convert.ToString(ViewState["PrevRecordFirstName"]) && e.Row.Cells[2].Text == Convert.ToString(ViewState["PrevRecordLastName"]))
            {
                e.Row.Cells[0].Text = "";
                e.Row.Cells[1].Text = "";
                e.Row.Cells[2].Text = "";
            }
            else
            {
                ViewState["PrevRecordStudentID"] = Convert.ToInt32(e.Row.Cells[0].Text) ;
                ViewState["PrevRecordFirstName"] = Convert.ToString(e.Row.Cells[1].Text);
                ViewState["PrevRecordLastName"] = Convert.ToString(e.Row.Cells[2].Text);
            }
        }
    }
//=====Add Code===========================


//=====Change Code===========================
 <asp:GridView ID="tbl_report" runat="server" AutoGenerateColumns="False" 
        GridLines="None" CssClass="CSSTable" onrowdatabound="tbl_report_RowDataBound">
//=====Change Code===========================