Egil Hansen
Egil Hansen

Reputation: 15598

How to set the "insert new row" as first row in DataGridView

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?

Upvotes: 7

Views: 36359

Answers (7)

jons911
jons911

Reputation: 1836

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    

Upvotes: 7

banifesto
banifesto

Reputation: 11

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

Upvotes: 1

While-E
While-E

Reputation: 1555

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!

Upvotes: -2

Jeff Corder
Jeff Corder

Reputation:

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.

Upvotes: 4

Dillie-O
Dillie-O

Reputation: 29745

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.

Upvotes: 1

xyz
xyz

Reputation: 27857

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

Upvotes: 1

Jeremy Bade
Jeremy Bade

Reputation: 511

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.

Upvotes: 1

Related Questions