如何在asp.net中的数据表顶部添加新行? [英] How to add new row on top of the datatable in asp.net ?

查看:61
本文介绍了如何在asp.net中的数据表顶部添加新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用select查询并填入datatable,在该数据表中我需要在dataable上添加新行,一旦添加并需要在gridview中绑定,我的代码如下



  Dim  con  As  SqlConnection 
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim fdatas As DataTable
con = SqlConnection(ConfigurationManager。 ConnectionStrings( ConnectionString)。ToString())
cmd = SqlCommand
da = SqlDataAdapter
cmd.CommandText = 从AnnotationMark中选择不同的[UserName],[filePath],[document_filename],其中[filePath] =' + filepath + '

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

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


如果 fdatas.Rows.Count> 0 然后
Dim dr As DataRow = fdatas.NewRow()
dr.Table( 0 )( UserName)= 查看全部
dr.Table( 0 )( filePath)= filepath
dr.Table( 0 )( document_filename)= 查看全部
fdatas.Rows.Add(dr)

结束 如果

gridAnnotation.DataSource = fdatas
gridAnnotation.DataBind()





在fdatas数据表中我得到5个版本,我尝试在fdatas数据表之上添加新行,所有值都发生了什么?只添加新行只能在fdatas数据表中看到。



那么如何在现有值的fdatas之上添加新行。



如果我使用下面的代码,它会在fdatas datatable的最后添加行



 如果 fdatas.Rows.Count>  0  然后 
Dim dr As DataRow = fdatas.NewRow()
dr( UserName)= 查看全部
dr(< span class =code-string> filePath)= filepath
dr( document_filename)= 查看全部
fdatas.Rows.Add(dr)

结束 如果





注意:需要添加顶部的fdatas,所以需要指定位置

问候

Aravind

解决方案

你差不多了在这里,

 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[^]


这篇关于如何在asp.net中的数据表顶部添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆