为了账号安全,请及时绑定邮箱和手机立即绑定

使用GridView实现数据行的合并

使用GridView实现数据行的合并

慕仙森 2018-10-10 08:09:58
<table border="1" cellspacing="0"> <tbody> <tr> <td>商品名称</td> <td>价格(元)</td> <td>卡号</td> <td>密码</td> <td>购买时间</td> </tr> <tr> <td rowspan="3">魔兽亲情卡</td> <td>13</td> <td>1151111</td> <td>4444644</td> <td rowspan="3">2010.05.04</td> </tr> <tr> <td rowspan="2">12</td> <td>18111</td> <td>444434</td> </tr> <tr> <td>174111</td> <td>449634</td> </tr> <tr> <td>联通IP电话卡</td> <td rowspan="2">30</td> <td>dfs55</td> <td>dfds66</td> <td>2010.05.10</td> </tr> <tr> <td>QQ充值卡</td> <td>asdf</td> <td>ssss</td> <td>2010.05.08</td> </tr> <tr> <td>魔力卡</td> <td>55</td> <td>dfd</td> <td>asd</td> <td rowspan="3">2010.03.12</td> </tr> <tr> <td>仙剑奇侠传卡</td> <td rowspan="2">23</td> <td>dfs</td> <td>aaa</td> </tr> <tr> <td>跑跑卡丁车卡</td> <td>dddd</td> <td>sss</td> </tr> </tbody> </table> <p>使用GridView实现合并单元格效果!急,急</p>
查看完整描述

1 回答

?
holdtom

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;
                    }
                }
            }
        }
    }


查看完整回答
反对 回复 2018-10-23
  • 1 回答
  • 0 关注
  • 437 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信