首页 新闻 会员 周边

使用GridView实现数据行的合并

0
悬赏园豆:100 [已关闭问题]
<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>
逸夫的主页 逸夫 | 初学一级 | 园豆:0
提问于:2010-05-12 17:56
< >
分享
其他回答(4)
0

自己机器上有个例子,主要应用的是“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;
                    }
                }
            }
        }
    }

Astar | 园豆:40805 (高人七级) | 2010-05-12 20:37
辛苦你了!!!
支持(0) 反对(0) 逸夫 | 园豆:0 (初学一级) | 2010-05-15 18:25
0

像这种东西,一般我都用表格嵌套,没必要去弄什么合并单元格。除非精力特别充沛,时间特别多,脑子特别右。

路过秋天 | 园豆:4787 (老鸟四级) | 2010-05-13 11:46
0

html 自己布局

kevin_20131022 | 园豆:280 (菜鸟二级) | 2010-05-14 16:45
0

GridView先绑定数据源,然后把需要合并的列转化成模板,然后编辑模板,选择要合并的列,插入表格,放上label,,再然后源面板用‘<%#Eval()%>‘绑定列,再然后判断什么的  就不用我说了吧?还有什么问题,欢迎私聊~

bin.King | 园豆:138 (初学一级) | 2010-05-15 13:21
0

试了一楼回答的,不知道为什么绑定数据到Gridview后,获取到的列数是0,结果不执行下面的代码了。。明明有很多列的啊~

aqwsxcdgfgdfgdfgd | 园豆:44 (初学一级) | 2010-05-19 16:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册