views:

557

answers:

2

Hi, i have a data file(csv) consisting of 2 columns & 1000 rows, as i load it to my datagridview it takes alot of time, i just want to show only the first 6 rows just as a preview of file to user. Is there any way i can show only the first 6 rows in my datagrid view. Following is the code im displaying the data in DataGridView.

DataTable csvDataTable = CSVReader.ReadCSVFile(textBoxCsv.Text, true);
dataGridViewCsvData.DataSource = csvDataTable;
dataGridViewCsvData.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
+2  A: 

CSVReader is an open source project isn't it? try to add ReadTopLines method to that class that will read only top N lines given as parameter

ArsenMkrt
yes its an open source project. is there any way i can get top 6 lines of dataTable and then display it to dataGridView?
Umair
there are, but why you need to read all file if you are displaying only firs 6 lines? you can use linq to dataset for example or DataView to select first lines and show it, or at least copy first 6 lines, but you will lose performance on reading unused lines
ArsenMkrt
i got it, but now im having problem in reading an Excel file and the same situation i want to display only first 6 lines. Could you please tell me a Query to select the first 6 rows..the Query im using for Excel file is:Select * from [sheet1$]
Umair
try select * from [Sheet1$] limit 6
ArsenMkrt
its not working !!!
Umair
A: 

Every datatable has it's own DefaultView. http://msdn.microsoft.com/en-us/library/system.data.datatable.defaultview.aspx

You can then get the Table from the view by DefaultView.GetTable. And you can manipulate data in you View the way you want. You can filter up, query.

You can find out more about expressions here: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

OR, since CSVReader is an open-source project, you can simply change

public DataTable CreateDataTable(bool headerRow)

Add number of lines to this method, and you will get what you need without reading the whole file.

I didn't read the whole source, so there might be a solution without even changing a code.

Use Open Source for 100%. Change it, customize it, send you patches! People do appreciate it! And you will get experience, knowledge and new friends who might help you in future :)

ifesdjeen
thanks.. it was helpful :)
Umair
np. my pleasure
ifesdjeen
btw im suing this code:string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + textBoxCsv.Text + ";" + "Extended Properties= Excel 8.0";OleDbConnection OleConnection = new OleDbConnection(ConnectionString);OleDbDataAdapter OleAdapter = new OleDbDataAdapter("Select * from [sheet1$]", OleConnection);DataSet ds = new DataSet();OleAdapter.Fill(ds);dataGridViewCsvData.DataSource = ds.Tables[0];i want to get top 6 rows, how can i get them ?
Umair