Gridview控件合并tr和td显示数据解决方法

Gridview控件合并tr和td显示数据解决方法

 

通过gridview控件和代码的控制解决,代码如下

//合并tr显示数据

        private  static void GroupRows(GridView GridView1, int cellNum, int begin, int end)

        {

            int i = begin, rowSpanNum = 1;

            while (i < end)

            {

                GridViewRow gvr = GridView1.Rows[i];

                for (++i; i < end; i++)

                {

                    GridViewRow gvrNext = GridView1.Rows[i];

                    if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)

                    {

                        gvrNext.Cells[cellNum].Visible = false;

                        rowSpanNum++;

                    }

                    else

                    {

                        gvr.Cells[cellNum].RowSpan = rowSpanNum;

                        rowSpanNum = 1;

                        break;

                    }

 

                    if (i == GridView1.Rows.Count - 1)

                    {

                        gvr.Cells[cellNum].RowSpan = rowSpanNum;

                    }

                }

            }

        }

在方法中如下调用

GroupRows(Smalltype, 0, 0, Smallgoodstype.Count);

 

第一个0表示第几列,第二个0表示开始的行数

首先,我们需要建立一个数据库并创建一个数据表。可以使用以下 SQL 语句: ```sql CREATE DATABASE Exam; USE Exam; CREATE TABLE Applicants ( ID INT PRIMARY KEY, IDCard VARCHAR(18) NOT NULL, Name VARCHAR(50) NOT NULL, Education VARCHAR(20) NOT NULL, Experience VARCHAR(200) NOT NULL ); ``` 然后,我们需要在 HTML 页面中添加 GridView 控件显示数据。示例代码如下: ```html <!DOCTYPE html> <html> <head> <title>考试人员申报数据库</title> <style type="text/css"> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; border: 1px solid #ddd; } th { background-color: #f2f2f2; } </style> </head> <body> <h1>考试人员申报数据库</h1> <table> <tr> <th>ID</th> <th>身份证号</th> <th>姓名</th> <th>学历</th> <th>主要经历</th> </tr> <%-- 这里使用 ASP.NET 的语法来绑定数据 --%> <% string connectionString = "Data Source=YOUR_SERVER_NAME;Initial Catalog=Exam;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { string query = "SELECT * FROM Applicants WHERE Education='本科'"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { int id = reader.GetInt32(0); string idCard = reader.GetString(1); string name = reader.GetString(2); string education = reader.GetString(3); string experience = reader.GetString(4); %> <tr> <td><%=id%></td> <td><%=idCard%></td> <td><%=name%></td> <td><%=education%></td> <td><%=experience%></td> </tr> <% } } %> </table> </body> </html> ``` 在这个示例代码中,我们使用了 ASP.NET 的语法来访问数据库并绑定数据GridView 控件中。你需要将 `YOUR_SERVER_NAME` 替换为你的 SQL Server 实例的名称。同时,你需要根据实际情况修改数据库连接字符串和 SQL 查询语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值