以编程方式向 gridview 添加一行 VB

Adding a row to a gridview programmatically VB

VS 2013,VB。 ASP.NET

我的页面上有一个 gridview。

                     <asp:GridView ID="gvSteam" runat="server" AutoGenerateColumns="False" CssClass="gvSteam">
                            <Columns>
                                <asp:BoundField DataField="Generation" HeaderText="Steam Generation (Capacity)" ReadOnly="True" SortExpression="DisplayName" />

                                <asp:TemplateField HeaderText="TPH" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
                                    <ItemTemplate>
                                        <asp:Label ID="lblValueEnergy" runat="server" Text='<%# Format8888(Eval("Power")) %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>


                            </Columns>
                            <HeaderStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
                        </asp:GridView>

后面的代码如下

        Dim Asset As New Assets
    Dim Allsteam As New List(Of Assets)
    Allsteam = Asset.GetSteamGenerated()
    gvSteam.DataSource = Allsteam
    gvSteam.DataBind()

我想在特定点(例如第 4 行之后)向 gridview 添加一个空白行,并向其中添加一些文本。

我可以使用数据表作为示例,但我使用的是 gridview,并且更愿意这样做。

按以下方式排序,可能不是最好的方法,但很管用。

        Dim newRow As GridViewRow

    For Each newRow In gvSteam.Rows

        If newRow.RowIndex = 3 Then

            Dim row As New GridViewRow(4, 4, DataControlRowType.EmptyDataRow, DataControlRowState.Normal)
            Dim tc As New TableCell
            tc.ColumnSpan = 2
            Dim lbl As New Label
            tc.Controls.Add(lbl)
            lbl.Text = "Steam Turbine 11"
            row.Controls.Add(tc)
            gvSteam.Rows(4).Parent.Controls.AddAt(4, row)

        End If
    Next