.net 如何在 DataGridView 中将“插入新行”设置为第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/263913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the "insert new row" as first row in DataGridView
提问by Egil Hansen
I am trying to get the DataGridView to render the "insert new row" row as the first row in the grid instead of the last row. How do I go about doing that, is it even possible in the control?
我试图让 DataGridView 将“插入新行”行呈现为网格中的第一行而不是最后一行。我该怎么做,甚至有可能在控制中吗?
回答by jons911
I don't think there is any way to move the "new row" row to the top of the data grid.
我认为没有任何方法可以将“新行”行移动到数据网格的顶部。
But, what if you left the top row empty and as the data filled in move the row down as appropriate? In other words, make your own "new row" row, which is just first row in the grid and add new blank rows above when editing is over.
但是,如果您将第一行留空并且随着数据的填充将行适当地向下移动会怎样?换句话说,创建您自己的“新行”行,它只是网格中的第一行,并在编辑结束时在上方添加新的空白行。
Dim myrow = existingDataTable.NewRow
existingDataTable.Rows.Add(myrow)
adp.Fill(existingDataTable)
With DataGridView1
.DataSource =existingDataTable
End With
回答by Egil Hansen
If you are not using bound controls, dgvItems.Rows.Insert( ) will allow you to specify where the row is inserted.
如果您没有使用绑定控件,dgvItems.Rows.Insert() 将允许您指定行的插入位置。
If you are using a bound datagridview, without a sort active, add the new row to the data source in the desired location:
如果您使用的是绑定 datagridview,没有排序活动,请将新行添加到所需位置的数据源:
BoundTable.Rows.InsertAt(BoundTable.Rows.NewRow(), RowLocation);
BoundTable.Rows.InsertAt(BoundTable.Rows.NewRow(), RowLocation);
I have found an interesting relationship, if there are edits on the current row and the row has not been committed to the datasource (i.e. you just started entering the data), it seems to be necessary to move the active cell to another row to commit the changes before inserting rows.
我发现了一个有趣的关系,如果当前行有编辑并且该行没有提交到数据源(即你刚开始输入数据),似乎需要将活动单元格移动到另一行提交插入行之前的更改。
回答by Dillie-O
Are you consuming the RowDataBound event? If so, you could check to see if you are binding the column header row and then add the appropriate code to insert a row just below that.
您是否正在使用 RowDataBound 事件?如果是这样,您可以检查您是否正在绑定列标题行,然后添加适当的代码以在其下方插入一行。
That way when your content rows are bound, there isn't any confusion with item indexes being involved, though I would recommend using some DataKeys or something just in case. In addition, using the RowDatabound event would allow you to have an "insert row" available when the user pages to a different block of rows in the GridView.
这样,当您的内容行被绑定时,与所涉及的项目索引不会有任何混淆,尽管我建议使用一些 DataKeys 或其他东西以防万一。此外,当用户翻到 GridView 中的不同行块时,使用 RowDatabound 事件将允许您有一个“插入行”可用。
回答by Jeremy Bade
Can you assign a temporary value to the new row's key that is less than all of the existing rows? Then sort a DataView by that key and bind the view to your grid.
您能否为新行的键分配一个小于所有现有行的临时值?然后按该键对 DataView 进行排序并将视图绑定到您的网格。
My team uses negative numbers as row ids until that row has been inserted into the database. I don't recall our new rows showing up at the top but we also don't have that as a requirement.
我的团队使用负数作为行 ID,直到该行被插入到数据库中。我不记得我们的新行出现在顶部,但我们也没有将其作为要求。
回答by xyz
And if all else fails the hacky way would be to put a separate one line DataGridView above the existing one, clone its columns, and wire up the row submission event to add the data to the lower one :)
如果所有其他方法都失败了,那么hacky 的方法是在现有的 DataGridView 上方单独放置一行,克隆其列,并连接行提交事件以将数据添加到较低的行:)
回答by banifesto
As While-E said, use Insertinstead of Add. This way you can specify where you want to add the new record. To add a new record on top of the grid, just use Insert(0).
正如 While-E 所说,使用Insert代替Add. 通过这种方式,您可以指定要添加新记录的位置。要在网格顶部添加新记录,只需使用Insert(0).
DataGridView1.Rows.Insert(0)this will add a new record on top, always
DataGridView1.Rows.Insert(0)这将始终在顶部添加新记录
DataGridView1.Rows(0).Cells(n).Value = "abc"this will set the value at n column
DataGridView1.Rows(0).Cells(n).Value = "abc"这将在 n 列设置值
回答by While-E
I'm assuming you mean that when you want to add a new row, you want the row to actually be inserted at the top of the table instead of the bottom of the table like default? Well if this is so, you don't have to deal with sorting or key values; simply do as such with the Insert method:
我假设您的意思是当您要添加新行时,您希望该行实际插入表格的顶部而不是默认的表格底部?好吧,如果是这样,您就不必处理排序或键值;只需使用 Insert 方法这样做:
'Create a new row
Dim tmpRow = New DataGridViewRow
'Adjust the row in some way
tmpRow.Height = _cellRectSize
'Add the row to DataGridView(i.e dgvEditor) at the top(index = 0)
Me.dgvEditor.Rows.Insert(0, tmpRow)
'新建一行
Dim tmpRow = 新的 DataGridViewRow
'以某种方式调整行
tmpRow.Height = _cellRectSize
'将行添加到顶部的DataGridView(即dgvEditor)(索引= 0)
Me.dgvEditor.Rows.Insert(0, tmpRow)
Works like a charm for me, hope it helps someone else!
对我来说就像一个魅力,希望它可以帮助别人!

