views:

7862

answers:

5

Hi there

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?

Regards, Egil.

+3  A: 

I don't think there is anyway 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.

jons911
hmm that could work. I am not sure how it would work with with databinding etc., but I will have to experiment a bit with it.
Egil Hansen
+1  A: 

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.

Jeremy Bade
+1  A: 

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

frou
+1  A: 

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.

+1  A: 

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.

Dillie-O