1

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

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Narendra V
  • 595
  • 1
  • 8
  • 21

2 Answers2

0

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.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • Thanks campbell for reply. I've tried adding a row to datatable and rebind the datagrid with latest datatable, but no luck. The newly added row is adding to the last instead betwwen. – Narendra V Nov 23 '10 at 18:21
  • if you rebind what are you sorting by or is there an order by in your SQL generating the order? – Matt Nov 23 '10 at 18:32
  • Hi Campbell, can you please help me with one example. – Narendra V Nov 23 '10 at 18:33
  • If you want an example you should at least provide what language you are using(as Tag), VB.Net or C#. It would be helpful also to show some code you already have. – Tim Schmelter Nov 23 '10 at 21:04
0

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
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939