简体   繁体   中英

how to add new row in to a datagridview after the existing row

In c# how to add new row in to a datagridview after the existing row.

I tried grdshedule.Rows.Add(); But all the rows are adding before the existing row

Make sure that AllowUserToAddRows property of datagridview is False . And like u tried before programmaticly ;

datagridview.Rows.Add();
grdshedule.Rows.Insert(grdshedule.Rows.Count-1, newRow);

Your rows are not getting automatically sorted hence you cannot see the new row at the bottom, this means once you will have to resort your rows after adding new row

To sort please example http://msdn.microsoft.com/en-us/library/0868ft3z.aspx

**Line Taken from MSDN Remarks**
Rows in the control are not automatically sorted when new rows are added.

Please read full remarks from MSDN http://msdn.microsoft.com/en-us/library/x0akdhcd.aspx

I had the same issue with DataGridView when it came to adding a new row after existing rows or at the end of the table.

After spending some time on this, I figured out why I couldn't add rows at the end of the table. It's not that the datagridview had any issues or restrictions, but it was really in my code. However, datagridview does add new row at the end of the table every time.

I really wish you posted some part of your code it will help a lot in giving you an answer.

Anyways, in my program I have a class from which I create list of objects. My List is actually an ArrayList and I display these objects in the same order they are in the list. Whenever I add or delete an object from the List, it is reflected 1-to-1 on the datagridview. In other word, if the objects were listed in reverse order in the ArrayList, then that's the order the objects were shown in the Datagridview. That doesn't mean the rows are being added on top of your existing rows, although it looked like it for me.

My guess is that you have a list of some sort that you are maintaining in your program. This list is where you get your information to display on your datagridview. If I am correct in saying that, then you probably are inserting your items in your list somewhere in your program based on the row that is currently selected on your datagridview. If that is the case, then your current item is being inserted into the position of your selected items in your ArrayList. This causes your list to shift everything by one from the selected item in your ArrayList. Thus, when you display your list of items in your datagridview, it looks like the new row is not being added at the end of the table.

To resolve this issue, first you need to add a new empty row:

DGrid.Rows.Add;

Then, insert your new item at the very end of your list:

var newitem := new Object;
ArrayList.insert(DGrid.RowCount-1,newitem);

Then, update your datagridview based on this ArrayList. Now, your datagridview will act as you expected.

Before my fix, my datagridview kept acting like it was inserting new rows on the top and in reverse order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM