在gridview中添加新行

时间:2013-05-29 15:47:08

标签: asp.net gridview datatable dataset

下一个代码显示了一个包含5列和多行的填充表(gridview)。它运行良好

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

在此代码之后,结果如下:

我有的表

Table that I have

现在我需要在第一个位置添加一个新行。第一列为空。必须组合第二列和第三列。与第4和第5相同。这是我需要的表格的图像:

我需要的表

Table that I need

此致

1 个答案:

答案 0 :(得分:0)

您需要使用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>

和Code Behind;

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();

        ...
        ..
        .
    }
}