如何在表中更新数据网格,并在任意位置插入行 [英] How can I update a data grid from table with row insertions in arbitrary locations

查看:56
本文介绍了如何在表中更新数据网格,并在任意位置插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于从链接数据表更新数据网格控件的问题,通常是一项简单的任务。



我有一个数据表,其中行可以插入到末尾以外的位置(索引)。数据网格始终显示网格末尾的插入。网格中的内容顺序对于此应用程序至关重要,并且必须与表格中的行顺序相匹配,包括表格插入。



表格从条目开始表格

a

b

c

d



然后是一个新的条目,表格看起来像

a

b

g(新条目)

c

d



然后网格看起来像

a

b

c

d

g



有谁知道如何进行网格更新,以便它反映顺序插入任意位置(索引)后表中的行?在示例中,网格应显示g跟随b,而不是结尾。



编码在VB中。



所有欢迎回复。

I have a question about updating a Data Grid control from a linked Data Table, usually an easy task.

I have a data table where rows can be inserted at a location (index) other than at the end. The data grid always shows the insertion at the end of the grid. The order of contents in the grid is critical for this application and must match the order of rows in the table including the table insertions.

The table starts out with entries of the form
a
b
c
d

Then along comes a new entry and the table can look like
a
b
g (new entry)
c
d

The grid will then look like
a
b
c
d
g

Does anyone know how to make the grid update so that it reflects the order of the rows in the table after insertion at an arbitrary location (index)? In the example, the grid should show g following b, not at the end.

Coding is in VB.

All replies welcome.

推荐答案

尝试使用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.


这篇关于如何在表中更新数据网格,并在任意位置插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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