且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在asp.net中的数据表顶部添加新行?

更新时间:2023-12-01 19:35:46

你差不多了在这里,

 fdatas.Rows.InsertAt(dr,0); 



DataRowCollection.InsertAt Method [ ^ ]


Hi I am use select query and fill in datatable,in that datatable i need to add new row on top of the dataable,once add and need to bind in gridview,my code as follows

Dim con As SqlConnection
      Dim cmd As SqlCommand
      Dim da As SqlDataAdapter
      Dim fdatas As New DataTable
      con = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString())
      cmd = New SqlCommand
      da = New SqlDataAdapter
      cmd.CommandText = "select distinct [UserName],[filePath],[document_filename]  from AnnotationMark where [filePath]='" + filepath + "' "

      cmd.Connection = con
      da.SelectCommand = cmd
      con.Open()

      da.Fill(fdatas)
      con.Close()
      con.Dispose()
      da.Dispose()


      If fdatas.Rows.Count > 0 Then
          Dim dr As DataRow = fdatas.NewRow()
          dr.Table(0)("UserName") = "View All"
          dr.Table(0)("filePath") = filepath
          dr.Table(0)("document_filename") = "View All"
          fdatas.Rows.Add(dr)

      End If

      gridAnnotation.DataSource = fdatas
      gridAnnotation.DataBind()



In fdatas datatable i get 5 vlaues,and i try to add new row on top of fdatas datatable,what happen all values diappear only added new row only can see in fdatas datatable.

So how to add new row on top of fdatas with existing values.

If i use following code it add row at last of fdatas datatable

If fdatas.Rows.Count > 0 Then
           Dim dr As DataRow = fdatas.NewRow()
           dr("UserName") = "View All"
           dr("filePath") = filepath
           dr("document_filename") = "View All"
           fdatas.Rows.Add(dr)

       End If



Note: need to add top of the fdatas ,so need to specify position
Regards
Aravind

You are almost there,
fdatas.Rows.InsertAt(dr, 0);


DataRowCollection.InsertAt Method[^]