1 回答
TA贡献1805条经验 获得超10个赞
自己机器上有个例子,主要应用的是“TableCell”。
GridView控件中自动求和、合并单元格和排序
1.前台
<table align="center" border="1" cellpadding="0" cellspacing="0">
<tr>
<td style="font-size: 9pt; color: #ff0000; text-align: center; height: 16px;">
学生成绩信息</td>
</tr>
<tr>
<td style="text-align: center">
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
CellPadding="4" Font-Size="9pt" ForeColor="#333333" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="stuname" HeaderText="学生姓名" SortExpression="stuname">
<ControlStyle Font-Underline="False" />
</asp:BoundField>
<asp:BoundField DataField="stusex" HeaderText="学生性别" SortExpression="stusex">
<ControlStyle Font-Underline="False" />
</asp:BoundField>
<asp:BoundField DataField="which_lesson" HeaderText="考试科目" SortExpression="which_lesson">
<ControlStyle Font-Underline="False" />
</asp:BoundField>
<asp:BoundField DataField="taotiname" HeaderText="套题名称" SortExpression="taotiname">
<ControlStyle Font-Underline="False" />
</asp:BoundField>
<asp:BoundField DataField="res_total" HeaderText="分数" SortExpression="res_total" />
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="font-size: 9pt; text-align: center">
请输入您要查找的学生姓名:<asp:TextBox ID="TextBox1" runat="server" Width="86px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="查询" /></td>
</tr>
</table>
2.后台
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=db_04;Uid=sa;Pwd=";
private int sum = 0;//取指定列的数据和
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortOrder"] = "stuname";
ViewState["OrderDire"] = "ASC";
bind();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
bind();
}
public void bind()
{
string sqlstr = "select * from tb_StuResult";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
myda.Fill(myds, "tb_StuResult");
DataView view = myds.Tables["tb_StuResult"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
GridView1.DataSource = view;
GridView1.DataBind();
GridView1.ShowFooter = false;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToInt32(e.Row.Cells[4].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "总分为:";
e.Row.Cells[2].Text = sum.ToString();
e.Row.Cells[3].Text = "平均分为:";
e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
GridView1.ShowFooter = true;
string sqlstr = "select * from tb_StuResult where stuname='" + TextBox1.Text.Trim() + "'";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
myda.Fill(myds, "tb_StuResult");
GridView1.DataSource = myds;
GridView1.DataBind();
gvRender();
}
else
{
Response.Write("<script>alert('请输入要查询的学生姓名')</script>");
}
}
private void gvRender()
{
if (GridView1.Rows.Count <= 1)
{
return;
}
for (int i = 0; i < GridView1.Columns.Count; i++)
{
TableCell oldtc = GridView1.Rows[0].Cells[i];
for (int j = 1; j < GridView1.Rows.Count; j++)
{
TableCell newtc = GridView1.Rows[j].Cells[i];
if (newtc.Text == oldtc.Text)
{
newtc.Visible = false;
if (oldtc.RowSpan == 0)
{
oldtc.RowSpan = 1;
oldtc.RowSpan = oldtc.RowSpan + 1;
oldtc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldtc = newtc;
}
}
}
}
}
- 1 回答
- 0 关注
- 437 浏览
添加回答
举报