المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem with removing columns and rows in dataGridView



C# Programming
03-31-2009, 01:25 AM
Hi there,

I’m trying to write a program that reads .xls file, formats it by removing specific rows and columns into table, sorts the data and then displays the data in dataGridView.

The .xls file is not formatted into table and in order to do that I need to delete 4 first rows and 2 first columns.

And here’s the problem when I’m trying to do that the program removes only 2 first rows and columns one and three instead of one and two. Here’s the code:


//opens an .xls file, creates data table, fills the dataGridView with the data from dataTable object
DataTable sampleDataTable = new DataTable();

OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\file.xls;Extended Properties=Excel 8.0");

aConnection.Open();

OleDbDataAdapter oleDbCommand = new OleDbDataAdapter("Select * from [file$]", aConnection);
oleDbCommand.Fill(sampleDataTable);

dataGridView1.DataSource = sampleDataTable;

//formats file into table by removing rows and columns at specified index
dataGridView1.Columns.RemoveAt(0);
dataGridView1.Columns.RemoveAt(1);

dataGridView1.Rows.RemoveAt(0);
dataGridView1.Rows.RemoveAt(1);
dataGridView1.Rows.RemoveAt(2);
dataGridView1.Rows.RemoveAt(3);


I don’t know what’s wrong as I tested it several times by programatiacally counting coulumns and rows but it’s still not working. It does work when I format the file before opening it by the program though.

Hope that somebody will point me in the right direction.

Thanks, Darius