在网上找的,都是一个列的和,
但我现在要显示的是学号,姓名,总分。总分那里是很多门课的和。
给大家看看我一条记录
学号,姓名,英语,物理,数学,体育。
这就是一条记录了,我现在要用GridView 显示出学号,姓名,总分这三项,
现在不明白的是,总分怎么处理,总分没有字段,怎么办,我知道要把英语,物理,数学,体育相加
然后得的值怎么传给总分,总分没有字段呀。
求高手指教。
但我现在要显示的是学号,姓名,总分。总分那里是很多门课的和。
给大家看看我一条记录
学号,姓名,英语,物理,数学,体育。
这就是一条记录了,我现在要用GridView 显示出学号,姓名,总分这三项,
现在不明白的是,总分怎么处理,总分没有字段,怎么办,我知道要把英语,物理,数学,体育相加
然后得的值怎么传给总分,总分没有字段呀。
求高手指教。
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="col1" DataField="col1" />
<asp:BoundField HeaderText="col2" DataField="col2" />
<asp:TemplateField>
<HeaderTemplate>
col3
</HeaderTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("col1", System.Type.GetType("System.String"));
dt.Columns.Add("col2", System.Type.GetType("System.String"));
dt.Columns.Add("col3", System.Type.GetType("System.String")); DataRow dr1 = dt.NewRow();
dr1[0] = "test1 in here";
dr1[1] = "there is no test1"; DataRow dr2 = dt.NewRow();
dr2[0] = "test2 in here";
dr2[1] = "there is no test2"; dt.Rows.Add(dr1);
dt.Rows.Add(dr2); GridView1.DataSource = dt;
GridView1.DataBind();
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = e.Row.Cells[0].Text + e.Row.Cells[1].Text;
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = e.Row.Cells[0].Text + e.Row.Cells[1].Text;
}
}
Height="203px" onrowdatabound="GridViewResult_RowDataBound" Width="413px">
<Columns>
<asp:BoundField DataField="SNO" HeaderText="学号" />
<asp:BoundField DataField="SNAME" HeaderText="姓名" />
<asp:BoundField DataField="ENGLISH" HeaderText="英语" />
<asp:BoundField DataField="MATH" HeaderText="数学" />
<asp:BoundField DataField="SPORT" HeaderText="体育" />
<asp:BoundField DataField="TOTAL" HeaderText="总分" />
</Columns>
</asp:GridView>
后台代码:protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["sqlConnectionStr"]))
{
SqlCommand comd = new SqlCommand("SELECT SNO,SNAME,ENGLISH,MATH,SPORT FROM RESULTS", con);
SqlDataAdapter sqlDa = new SqlDataAdapter(comd);
con.Open();
DataTable dt = new DataTable();
sqlDa.Fill(dt);
dt.Columns.Add(new DataColumn("TOTAL",System.Type.GetType("System.String")));
GridViewResult.DataSource = dt.DefaultView;
GridViewResult.DataBind();
}
}
} protected void GridViewResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!(string.IsNullOrEmpty(e.Row.Cells[2].Text) && e.Row.Cells[3].Text != null && e.Row.Cells[4].Text != null))
{
e.Row.Cells[5].Text = (int.Parse(e.Row.Cells[2].Text) + int.Parse(e.Row.Cells[3].Text)
+ int.Parse(e.Row.Cells[4].Text)).ToString();
}
}
}
这个方法蛮方便的,或者这样也可以,你在gridview中用一个模板列,然后在前台代码的源中绑定数据时,把这四项成绩的值相加即可.
model的时候多加个总分字段,然后写sql的时候,把相加后的总分赋值给那个model里面的总分。
Height="203px" onrowdatabound="GridViewResult_RowDataBound" Width="413px">
<Columns>
<asp:BoundField DataField="SNO" HeaderText="学号" />
<asp:BoundField DataField="SNAME" HeaderText="姓名" />
<asp:BoundField DataField="TOTAL" HeaderText="总分" />
</Columns>
</asp:GridView>