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>
是的,我可以計算查詢中的百分比,但是我還需要根據在我的Grades類中完成的百分比(A,B,C,D,F)來計算字母等級。 – ShoeLace1291
@ ShoeLace1291 - 使用gridview rowdatabound事件,當數據行綁定到gridview控件中的數據時,將觸發此事件,這將允許您在綁定到網格時修改數據..這將幫助您開始。 [msdn rowdatabound](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v = vs.110).aspx) – Roy
我該怎麼做? – ShoeLace1291