在两行之间插入行

发布于 2024-10-04 04:40:56 字数 113 浏览 0 评论 0原文

我们使用 ASP.NET 1.1 进行网站开发,使用 DataGrid 控件显示数据。

有人可以建议我如何在数据网格控件中的两行之间插入行吗?

谢谢,

-纳伦德拉

We are using ASP.NET 1.1 for a website development, using DataGrid control for showing data.

Can some body suggest how can i insert rows in between two rows in datagrid control.

Thanks,

-Narendra

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-10-11 04:40:56

您必须将该行插入数据源(可能是数据表)中。

然后,您必须确保网格按您的要求排序。您希望新行位于特定位置,因此也许可以在 DataTable 上实现 SortOrder 列,并根据您的要求显式更新新行下方的所有行该列中的值,在绑定之前。

You'll have to insert that row in your data source - perhaps a DataTable.

Then you'll have to ensure that the grid is sorted as per your requirement. You're wanting the new row in a specific spot, so perhaps implement a SortOrder column on your DataTable, and explicitly update the values in that column for all the rows below the new row, as per your requirements, before binding.

笑梦风尘 2024-10-11 04:40:56

您可以将新行插入到数据源(例如数据表)。
DataRowCollection 有一个函数 InsertAt ,可让您插入位于您想要的位置的行。

我将提供一个简单的示例(在带有 GridView 的 VB.Net 中,DataGrid 的工作原理相同):

ASPX:

 <asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="true">
        </asp:GridView>
        <asp:Button ID="BtnInsert" runat="server" Text="insert Obama at Position" /><asp:TextBox ID="TxtPosition" Text="0" runat="server"></asp:TextBox>

代码隐藏:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData(getDataSource())
        End If
    End Sub

    Private Function getDataSource() As DataTable
        Dim tbl As New DataTable
        Dim col As New DataColumn("ID", GetType(Int32))
        tbl.Columns.Add(col)
        col = New DataColumn("FirstName", GetType(String))
        tbl.Columns.Add(col)
        col = New DataColumn("LastName", GetType(String))
        tbl.Columns.Add(col)
        Dim row As DataRow = tbl.NewRow
        row("ID") = 1
        row("FirstName") = "Benjamin"
        row("LastName") = "Franklin"
        tbl.Rows.Add(row)
        row = tbl.NewRow
        row("ID") = 2
        row("FirstName") = "Arnold"
        row("LastName") = "Schwarzenegger"
        tbl.Rows.Add(row)
        row = tbl.NewRow
        row("ID") = 3
        row("FirstName") = "Albert"
        row("LastName") = "Einstein"
        tbl.Rows.Add(row)
        row = tbl.NewRow
        row("ID") = 4
        row("FirstName") = "Bill"
        row("LastName") = "Gates"
        tbl.Rows.Add(row)
        Return tbl
    End Function

    Private Sub BindData(ByVal source As DataTable)
        Me.MyGrid.DataSource = source
        Me.MyGrid.DataBind()
    End Sub

    Private Sub BtnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
        Dim pos As Int32 = 0
        Int32.TryParse(TxtPosition.Text, pos)
        Dim source As DataTable = Me.getDataSource()
        If pos < 0 OrElse pos > source.Rows.Count Then pos = 0
        Dim row As DataRow = source.NewRow
        row("ID") = 5
        row("FirstName") = "Barack"
        row("LastName") = "Obama"
        source.Rows.InsertAt(row, pos)'this is the only important'
        BindData(source)
    End Sub

You can insert the new row to the datasource (for example a DataTable).
DataRowCollection has a Function InsertAt that lets you insert rows at the position you want.

I will provide a simple example (in VB.Net with a GridView, DataGrid works the same):

ASPX:

 <asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="true">
        </asp:GridView>
        <asp:Button ID="BtnInsert" runat="server" Text="insert Obama at Position" /><asp:TextBox ID="TxtPosition" Text="0" runat="server"></asp:TextBox>

Codebehind:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData(getDataSource())
        End If
    End Sub

    Private Function getDataSource() As DataTable
        Dim tbl As New DataTable
        Dim col As New DataColumn("ID", GetType(Int32))
        tbl.Columns.Add(col)
        col = New DataColumn("FirstName", GetType(String))
        tbl.Columns.Add(col)
        col = New DataColumn("LastName", GetType(String))
        tbl.Columns.Add(col)
        Dim row As DataRow = tbl.NewRow
        row("ID") = 1
        row("FirstName") = "Benjamin"
        row("LastName") = "Franklin"
        tbl.Rows.Add(row)
        row = tbl.NewRow
        row("ID") = 2
        row("FirstName") = "Arnold"
        row("LastName") = "Schwarzenegger"
        tbl.Rows.Add(row)
        row = tbl.NewRow
        row("ID") = 3
        row("FirstName") = "Albert"
        row("LastName") = "Einstein"
        tbl.Rows.Add(row)
        row = tbl.NewRow
        row("ID") = 4
        row("FirstName") = "Bill"
        row("LastName") = "Gates"
        tbl.Rows.Add(row)
        Return tbl
    End Function

    Private Sub BindData(ByVal source As DataTable)
        Me.MyGrid.DataSource = source
        Me.MyGrid.DataBind()
    End Sub

    Private Sub BtnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
        Dim pos As Int32 = 0
        Int32.TryParse(TxtPosition.Text, pos)
        Dim source As DataTable = Me.getDataSource()
        If pos < 0 OrElse pos > source.Rows.Count Then pos = 0
        Dim row As DataRow = source.NewRow
        row("ID") = 5
        row("FirstName") = "Barack"
        row("LastName") = "Obama"
        source.Rows.InsertAt(row, pos)'this is the only important'
        BindData(source)
    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文