Hi all.
I have a bit of a nasty task on hand at the moment. I have a datagridview which when loaded gets its data source from an Excel Spreadsheet. Now that part works fine. However, the idea behind this specific form is to be able to clean the spread sheet up and make certain changes to it. I know that the user can do it in excel, however, There are some things which need to be done by automation and not manually.
For example. The idea here is that when a user gets an age analysis from Quickbooks, the spread sheet which it pumps the data to, uses two lines for one client. however, We need to have only one line per client in the spread sheet which will then be used to import into another application. The problem is, that some of the clients have up to 5000 users in their system. (thus making the spread sheet 10 000 rows). No one in there right mind would "like" to sit and manually delete every second row. So now, I need to find a way to do it in an application.
Is there any one who has an idea on how I can go about deleting every second row in a dgv. Like I said, I can get the data from excel, I can display it in the dgv. Now, I need a button click event to be able to remove every second row. Once the alternating rows have been removed, I will then export the dgv to a new Excel Spread sheet. (No Prob there).
Thanks in advance.
Ricardo
Loading
Ricardo CarvalhoPosted Dec 6, 2007, 1:11 AM
It works perfectly. Thanks alot. really appreciate it.
Ricardo
AlanPosted Dec 5, 2007, 3:44 PM
Well, I think I'm right in saying that a bound DataGridView needs to be bound to a collection which implements IBindingList (such as a Dataview), with the AllowDelete property set to true, in oder to delete any of its rows programatically but, subject to that, the following code should delete alternate rows (i.e. rows with an index of 1,3,5 etc).
Notice that we have to work from the end of the Rows collection to avoid disturbing the indices of the rows we haven't yet deleted and also need to be careful not to delete the final row which, of course, is used for adding new records.
The DataGridView is assumed to be called dataGridView1:
int
count = dataGridView1.Rows.Count - 1; // excluding last row int final; // index of final row to delete - must be odd if (count % 2 == 0)final = count - 1;
elsefinal = count;
for (int i = final; i >= 1 ; i -= 2){
dataGridView1.Rows.RemoveAt(i);
}