How to set the “insert new row” as first row in DataGridView

烂漫一生 提交于 2020-01-11 02:36:05

问题


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?


回答1:


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    



回答2:


If you are not using bound controls, dgvItems.Rows.Insert( ) will allow you to specify where the row is inserted.

If you are using a bound datagridview, without a sort active, add the new row to the data source in the desired location:

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.




回答3:


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.

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.




回答4:


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 :)




回答5:


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.

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.




回答6:


As While-E said, use Insert instead 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).

DataGridView1.Rows.Insert(0) this will add a new record on top, always

DataGridView1.Rows(0).Cells(n).Value = "abc" this will set the value at n column




回答7:


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:

'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)

Works like a charm for me, hope it helps someone else!



来源:https://stackoverflow.com/questions/263913/how-to-set-the-insert-new-row-as-first-row-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!