close

功能:GridView搭配SqlDataSource小精靈,但是手動加入一個計算總分欄位。

sum  

 

前端畫面:

加入總分欄位步驟:

1.按下編輯資料行 -> 2.TemPlateField -> 3.加入 -> 4.改HeaderText

 

重點:

1.這邊可能要手動輸入程式碼將它轉成ItemTemplate樣板,不然沒辦法控制。

2.要DataBinding Expression,這裡用到副程式去算完後再回傳總分(ComputeTotal_NO()是副程式名子)

<asp:TemplateField HeaderText="總分">
<ItemTemplate>
<font color=red><b>
<%# ComputeTotal_NO() %></b></font>
</ItemTemplate>
</asp:TemplateField>

 

後端程式:

//用來每次累加算第幾行的i
public int i = 0;

protected void Page_Load(object sender, EventArgs e)
{

}

//計算總分的副程式,使用DataSet
public int ComputeTotal_NO()
{
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter myAdapter = new SqlDataAdapter("select * from student_test",Conn);

DataSet ds = new DataSet();

//取名為s_test 資料表
myAdapter.Fill(ds, "s_test");

int chinese,math,total;
chinese = Convert.ToInt32(ds.Tables["s_test"].Rows[i]["chinese"]);
math = Convert.ToInt32(ds.Tables["s_test"].Rows[i]["math"]);
total = chinese + math;

i++;

return total;
}

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 melomelo1988 的頭像
    melomelo1988

    melo 唐

    melomelo1988 發表在 痞客邦 留言(0) 人氣()