Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys ,
i have a data grid and i Want To add Index Column to it , so i added a column to the data table attached to it and I Set Some Properties to this column like

C#
DataColumn DC = new DataColumn("Index", typeof(int));
            this.DT_DataSource_AllOrderItems.Columns.Add(DC);
            this.DT_DataSource_AllOrderItems.Columns["Index"].AutoIncrement = true;
            this.DT_DataSource_AllOrderItems.Columns["Index"].AutoIncrementStep = 1;
            this.DT_DataSource_AllOrderItems.Columns["Index"].AutoIncrementSeed = 1;           


and when user delete Row the index does not updated , and i want it to be updated when both adding and deleting any one has any advice to do that

thanks
Posted
Comments
Bala Selvanayagam 13-Oct-11 8:10am    
Question not clear

1 solution

Not how indexes work unfortunately - so may have more luck using triggers - a trigger on add/delete that updates an auto increment in another table dedicated for this purpose.

However, if you need some specific ordering of indexes to be maintained with deletes as well, it may be that an auto-incrementing index is the wrong object for the task - you may need a dedicated column for this kind of logic.

Have you got a logical example of the problem?
 
Share this answer
 
Comments
Yasser El Shazly 13-Oct-11 8:31am    
it is just an index for Rows which added / deleted to the grid
Dave Kerr 13-Oct-11 9:26am    
An index doesn't have anything to do with deleted rows - it's just used as a unique key for rows in a table, only adding new rows needs to update the index - what are you expecting to happen when the row is deleted?

Row Number Index Example Column
1 1 Dogs
2 2 Cats
3 3 Fish

If you delete row 2, the indexes are (in order)
Row Number Index Example Column
1 1 Dogs
2 3 Fish

The indexes mustn't change! If they did, then anything that was using the index as a key (for example 'PetOwner' table has link to the table above) would be broken (e.g. a PetOwner of a Fish now owns something different!)

What is it exactly that you want the index to do? You can't use it as the 'number' of the row!
Yasser El Shazly 13-Oct-11 9:49am    
I just Want To add Row To Count Rows Added deleted to from the data grid
and i make a column with the properties i mentioned in previous but when i delete row as you say it becomes 1 , 3 not updated to 1 , 2 as you Mentioned in the column Row which updated from 1 , 2 , 3 --> and after deletion it became 1 , 2 in my case if i delete it became 1 , 3 (that is the problem and i do not know how can i solve it )
Dave Kerr 13-Oct-11 11:24am    
Right, well you do not use the index or primary key for this - if the primary key changes then it's not a key and it'll break database relationships. You need to add an extra column called 'row number' and keep this up to date yourself - if you delete a record with row number x, you must decrement every row number > x.

Why do you need the row number? It is generally not a sensible thing to store in a database, because the more records you have, the more processing is required to keep the row number up to date, this is a maintainance nightmare.

T-SQL has ROW_NUMBER()
http://msdn.microsoft.com/en-us/library/ms186734.aspx
but this will only number the results of a query. Are you sure you need the row number exactly? what are you using it for?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900