且构网

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

如何在表中更新数据网格,并在任意位置插入行

更新时间:2023-11-14 23:26:46

尝试使用DataView对记录进行排序,然后绑定数据网格。

Try to sort the records using DataView and then bind the datagrid.
Dim dataView As New DataView(yourDataTable)

dataView.Sort = "YourColumnName"

Dim newdataTable AS DataTable = dataView.ToTable()






更新:

道歉,我有完全误解了这个问题。

检查以下方法是否有助于解决您的问题。



如果您希望按插入顺序显示行,我们可以在数据表中添加新列以跟踪此情况。



1.添加名为SlNo的新列。您可以在从DB中回收数据时使用 ROW_NUMBER()确定SlNo

2.插入新行时,只需更新所有行中的SlNo值of GridView RowIndex。

3.在SlNo上对数据表进行排序



如果不起作用,请告诉我。

谢谢!



UPDATE :
My apologies, I have misunderstood the question completely.
Check if following approach can help resolving your problem.

As you want the rows to be displayed in order of their insertion, we can add new column in the datatable to keep track of this.

1. Add a new column named "SlNo". You can determine the "SlNo" using ROW_NUMBER() while retriving data from DB
2. While inserting a new row just update value of SlNo in all the rows with the value of GridView RowIndex.
3. Sort the datatable on "SlNo"

Please let me know if it doesn't work.
Thanks !


'花了很多实验来完成这个程序!



昏暗TempTable作为新数据表

'调用子例程,它定义临时表的列,使其匹配'真实表,但是没有定义任何键。

Dim DR As DataRow

我的整数= 0到InsertLocation - 1

DR = TempTable.NewRow

DR.Item(COLUMN1)= RealTable.Row( i)(专栏)

...

TempTable.Rows.Add(DR)

下一页我是



'创建要插入的新行

Dim NewRow as DataRow

NewRow = T empTable.NewRow

NewRow.Item(Column1)= newvalue1

......

插入新行

TempTable.Rows.Add(NewRow)



'现在将剩余的原始行复制到TempTable

对于j为Integer = InsertLocation到RealTable.Rows.Count-1

'和以前一样,创建一个匹配的数据行并添加到TempTable。

下一页j



'现在清空原始表(RealTable)

对于每个r作为RealTable中的DataRow

r.Delete

下一页



RealTable.AccptChanges的关键操作,没有这个就行不通!



'现在创建一组来自TempTable的新行和。将它们添加到RealTable。

Dim RDR as DataRow

对于i,整数= 0到TempTable.Rows.Count

RDR = RealTable.NewRow

RDR.Item(Column1)= TempTable.Row(i)(Column1)

...

RealTa ble.Rows.Add(RDR)

下一页我



'做完了。
'It took a lot of experimenting to come up with this procedure!

Dim TempTable as New DataTable
'Call Subroutine that defines the columns of the temp table so that it matches the 'Real table, but has NO keys defined.
Dim DR As DataRow
For i as Integer = 0 to InsertLocation - 1
DR = TempTable.NewRow
DR.Item(COLUMN1) = RealTable.Row(i)(Column)
...
TempTable.Rows.Add(DR)
Next i

'Create the new row to be inserted
Dim NewRow as DataRow
NewRow = TempTable.NewRow
NewRow.Item(Column1) = newvalue1
......
Insert the new row
TempTable.Rows.Add(NewRow)

'Now copy rest of original rows to the TempTable
For j as Integer = InsertLocation to RealTable.Rows.Count-1
'As before, create a matching data row and add to TempTable.
Next j

'Now Empty the original table (RealTable)
For each r as DataRow in RealTable
r.Delete
Next

RealTable.AccptChanges 'CRITICAL Operation, won't work without this!

'Now create a set of new rows from the TempTable and .Add them to the RealTable.
Dim RDR as DataRow
For i as integer = 0 to TempTable.Rows.Count
RDR = RealTable.NewRow
RDR.Item(Column1) = TempTable.Row(i)(Column1)
...
RealTable.Rows.Add(RDR)
Next i

'Done at long last.