*//Hide 'StuID' cloumn
protected void gvGrade_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[2].Visible = false;
//if gvGrade.AutoPaging=true It's something wrong.....
}
... {
gvGrade.EditIndex = e.NewEditIndex;
gvGrade.DataSource = CreateDataSource();
gvGrade.DataBind();
}
protected void gvGrade_RowUpdating( object sender, GridViewUpdateEventArgs e)
... {
OleDbConnection updateConn = new OleDbConnection(connString);
updateConn.Open();
In VS2005 It's very difficult to fetch the content of Hidden Column , so Fetch it from DataSet#region In VS2005 It's very difficult to fetch the content of Hidden Column , so Fetch it from DataSet
string idSql = "SELECT distinct StuID FROM Grade where Department = '" + ddDepart.SelectedItem.Value.ToString() + "'";
OleDbDataAdapter idAdapter = new OleDbDataAdapter(idSql, updateConn);
DataSet idDs = new DataSet();
idAdapter.Fill(idDs, "id");
#endregion
Calculate "colCount" because in "Page_Load" has "if(!IsPostBack)#region Calculate "colCount" because in "Page_Load" has "if(!IsPostBack)
string calcSql = "SELECT COUNT(CourseName) FROM Course where Department = '"
+ ddDepart.SelectedItem.Value.ToString() + "'";
OleDbCommand calcCmd = new OleDbCommand(calcSql, updateConn);
int colCount = (int)calcCmd.ExecuteScalar();
#endregion
Define a arraylist to save "CourseName"#region Define a arraylist to save "CourseName"
ArrayList tempAl = new ArrayList();
for (int i = 3; i < colCount+3; i++)
...{
tempAl.Add(gvGrade.HeaderRow.Cells[i].Text);
}
#endregion
Execute Update#region Execute Update
for (int j = 0; j < colCount; j++)
...{
//get the data from "TextBox"
string getScore = ((TextBox)(gvGrade.Rows[e.RowIndex].Cells[j+3].Controls[0])).Text.Trim();
if (getScore == "")
...{
Response.Write("<script language='javascript'>alert('分数不可为空!');</script>");
break;
}
else
...{
string updateSql = "update Grade set Score=" + Int32.Parse(getScore)
+ " where StuID=" + idDs.Tables[0].Rows[e.RowIndex][0]
+ " and Course= " + "'" + (string)tempAl[j] + "'";
OleDbCommand updateCmd = new OleDbCommand(updateSql, updateConn);
updateCmd.ExecuteNonQuery();
}
}
#endregion
updateConn.Close();
}
*显示不及格分数:(使用了“清清月儿”的办法http://blog.csdn.net/21aspnet/ 十分感谢!)
... {
for (int i = 0; i < gvGrade.Rows.Count; i++)
...{
for (int j = 1; j < gvGrade.Rows[i].Cells.Count; j++)
...{
string score = gvGrade.Rows[i].Cells[j].Text.Trim();
if (!System.Text.RegularExpressions.Regex.IsMatch(score, @"^[0-9]+$"))
continue;
if (Convert.ToDouble(score) < 60)
...{
//If < 60 change bgcolor for the cell & ID
gvGrade.Rows[i].Cells[j].BackColor = System.Drawing.Color.Red;
gvGrade.Rows[i].Cells[0].BackColor = System.Drawing.Color.Red;
}
}
}
*点击学号显示学生详细信息(超链接列) //学会用Eval了,^_^
admin.aspx:
<asp:CommandField CancelText="取消" DeleteText="删除" EditText="编辑" ShowEditButton="True"
UpdateText="确定" >
<ItemStyle Width="80px" />
<ControlStyle Width="80px" />
</asp:CommandField>
<asp:TemplateField HeaderText="学号">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl=<%#"./StuInfo.aspx?StuId="+Eval("学号") %>
Text='<%# Eval("学号") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>