【问题标题】:Add a new row in a gridview在网格视图中添加新行
【发布时间】:2013-05-24 23:35:49
【问题描述】:

下一个代码显示了一个填充了 5 列和几行的表格(一个网格视图)。运行良好

while (drCLientes.Read())
{
GridView gv = new GridView();
gv.ID = "myGridID";
cong.Open();
da = new OleDbDataAdapter(sql, cong);
ds = new DataSet("Clientes");
da.Fill(ds, "Clientes");
cong.Close();
gv.DataSource = ds.Tables["Clientes"];
gv.DataBind();
}

在这段代码之后,结果是这样的:

我有的表

现在我需要在第一个位置添加一个新行。第一列为空。第二列和第三列必须合并。第 4 次和第 5 次也是如此。这是我需要的带有表格的图像:

我需要的表

问候

【问题讨论】:

    标签: asp.net gridview datatable dataset


    【解决方案1】:

    您需要使用Repeater 而不是 GridView。

    <asp:Repeater ID="repList" runat="server" OnItemCommand="repList_ItemCommand">
        <HeaderTemplate>
            <table cellpadding="3" cellspacing="0" border="1" style="border-collapse: collapse;"
                width="100%">
                <thead style="text-align: center;" class="Gridheader">
                    <tr>
                        <td rowspan="2">Sequence</td>
                        <td colspan="4">Student</td>
                        <td colspan="3">Lesson</td>
                        <td rowspan="2">Point</td>
                    </tr>
                    <tr>
                        <td>Name</td>
                        <td>Surname</td>
                        <td>Age</td>
                        <td>Class</td>
                        <td>Name</td>
                        <td>Teacher</td>
                        <td>Time</td>
                    </tr>
                </thead>
                <tbody>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td align="center">
                    <asp:Label ID="lblSequence" runat="server" >
                    </asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblStudentName" runat="server" >
                </td>
                <td>
                    <asp:Label ID="lblStudentSurname" runat="server" >
                </td>
                <td>
                    <asp:Label ID="lblStudentAge" runat="server" >
                </td>
                <td>
                    <asp:Label ID="lblLessonClass" runat="server" >
                </td>
                <td>
                    <asp:Label ID="lblLessonName" runat="server" >
                </td>
                <td>
                    <asp:Label ID="lblLessonTeacher" runat="server" >
                </td>
                <td>
                    <asp:Label ID="lblLessonTime" runat="server" >
                </td>
                <td>
                    <asp:Label ID="lblLessonPoint" runat="server" >
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </tbody> 
        </table>
        </FooterTemplate>
    </asp:Repeater>
    

    和背后的代码;

    public void rptr_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Data.Common.DbDataRecord objRow = (System.Data.Common.DbDataRecord)e.Item.DataItem;
    
            Label lblAnswer = e.Item.FindControl("lblSequence") as Label;
            lblAnswer.Text = objRow["Sequence"].ToString();
    
            Label lblStudentName = e.Item.FindControl("lblStudentName") as Label;
            lblStudentName.Text = objRow["StudentName"].ToString();
    
            ...
            ..
            .
        }
    }
    

    【讨论】:

    • 我需要在代码视图中创建网格视图,因为我必须为数百个客户呈现 1 个表格和 1 个图表...我没有在设计器视图中创建控件...
    • 我尝试了 Furkan Ekinci 的想法。代码没有显示表格..我有问题。我需要在代码视图而不是设计器视图中编写代码。 while (drClients.Read()) { TableRow row1 = new TableRow(); table.Rows.Add(row1);表格单元格 1 = 新表格单元格(); row1.Cells.Add(cell1);中继器代表 = 新中继器(); rep.ID = "我的中继器";丛.Open(); OleDbCommand cmd = new OleDbCommand(sqlGrid, con); OleDbDataReader dr = cmd.ExecuteReader();代表数据源=博士;代表数据绑定(); cell1.Controls.Add(rep);博士关闭();丛。关闭(); }
    • 使用 Furkan Ekinci 的代码我只得到一个表link 但我有一个循环....我必须在代码视图中创建标题...link 我想我们是快结束了...问候
    • 您的表格看起来一样,因此您可以创建用户控件并将转发器代码替换为用户控件。
    • 你能告诉我从我的代码视图中调用循环中用户控件的方法吗?谢谢...
    猜你喜欢
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多