2016-03-03 22 views
0

我正在使用名为Students的数据库表的结果填充asp中的网格。学生表中列出了分数和分数可能。我需要根据积分/积分可能的百分比来计算字母等级。我在一个叫Grades的课上做这个。我如何获得当前学生的pointsEarned和pointsPossible的值,然后将计算出的letterGrade添加到网格中?如何在ASP中获取数据库记录时将自定义数据字段添加到GridView中?

这里是我的方法:

protected void Page_Load(object sender, EventArgs e) 
     { 
      string connstring; 
      connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString; 
      SqlConnection conn = new SqlConnection(connstring); 
      SqlCommand cmd = new SqlCommand("GetStudents", conn); 

      SqlDataAdapter adapter = new SqlDataAdapter(cmd); 

      conn.Open(); 

      DataSet ds = new DataSet(); 
      adapter.Fill(ds, "dbo.Students"); 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("letterGrade", typeof(string)); 

      Grades studentGrade = new Grades(); 
      studentGrade.pointsEarned = ???; 
      studentGrade.pointsPossible = ???; 
      string studentLetterGrade = studentGrade.calculate(); 

      //How do I add studentLetterGrade as the letterGrade column? 

      StudentGrid.DataSource = ds; 
      StudentGrid.DataBind(); 

      conn.Close(); 
      conn.Dispose(); 
     } 

这里是我的网格:

<asp:SqlDataSource ID="GetStudents" runat="server" ConnectionString="<%$ ConnectionStrings:dbConn %>" SelectCommand="dbo.GetStudents" SelectCommandType="StoredProcedure"></asp:SqlDataSource> 
     <asp:GridView ID="StudentGrid" runat="server" AutoGenerateColumns="False"> 
      <Columns> 
       <asp:CommandField ShowSelectButton="False" /> 
       <asp:BoundField DataField="studentId" HeaderText="ID" /> 
       <asp:BoundField DataField="firstName" HeaderText="First Name" /> 
       <asp:BoundField DataField="lastName" HeaderText="Last Name" /> 
       <asp:BoundField DataField="semester" HeaderText="Semester" /> 
       <asp:BoundField DataField="semesterYear" HeaderText="Year" /> 
       <asp:BoundField DataField="letterGrade" HeaderText="Grade" /> 
      </Columns> 
     </asp:GridView> 

回答

0

有这样做的,最简单的办法是很多的方法来计算在数据库存储过程本身的百分比检索并绑定到网格。

否则,如果你不想修改当前的存储过程,并希望在代码隐藏的绑定期间修改,你必须使用gridview rowdatabound事件,并将所有列逐个绑定,并根据条件修改值(如果需要的话)。

+0

是的,我可以计算查询中的百分比,但是我还需要根据在我的Grades类中完成的百分比(A,B,C,D,F)来计算字母等级。 – ShoeLace1291

+0

@ ShoeLace1291 - 使用gridview rowdatabound事件,当数据行绑定到gridview控件中的数据时,将触发此事件,这将允许您在绑定到网格时修改数据..这将帮助您开始。 [msdn rowdatabound](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v = vs.110).aspx) – Roy

+0

我该怎么做? – ShoeLace1291

相关问题