Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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

VB
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
Posted

1 solution

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

DataRowCollection.InsertAt Method[^]
 
Share this answer
 
v2
Comments
/\jmot 5-Feb-15 2:40am    
that's it.
i was going to post same answer.
+5
Peter Leow 5-Feb-15 5:14am    
Thank you, /\jmot.
Aravindba 5-Feb-15 3:05am    
yes thank u,already find solution using Rows.InserAt,any way thanx for ur effort.
Peter Leow 5-Feb-15 5:14am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900