Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write an sql query (in c#) to select excel sheet data in "C" column starting from C19. But i cant specify the ending cell number because more data are getting added to the column. Hence i need to know how to specify the end of the column. Please help. Given below is the code that I'm using. But it doesn't display any value in the datagrid

C#
OleDbCommand ccmd = new OleDbCommand(@"Select * From [SPAT$]", conn);
OleDbDataAdapter da = new OleDbDataAdapter(ccmd);
DataTable dt = new DataTable();
da.Fill(dt)

for (int i = 19; i < dt.Rows.Count; i++)
{
    var value = dt.Rows[i]["C"].ToString();
    dataGridView1.DataSource = value;
    
}
Posted
Updated 16-Oct-12 23:53pm
v2

Hi,

See the below links.

http://stackoverflow.com/questions/3814065/oledb-command-to-get-data-from-excel-sheet-uing-where-clause[^]
http://www.daniweb.com/software-development/csharp/threads/145027/excel-and-c[^]

you can use where condition with like and you can get only that records which you want.
like this :

C#
OleDbCommand ccmd = new OleDbCommand(@"Select * From [SPAT$] where [column Name] like 'C19*'", conn);


Thanks,
Viprat
 
Share this answer
 
Comments
hansikaat 17-Oct-12 6:44am    
Hi, thanks for the reply. What i need to do is to select all the data in the column C after C19 and insert them to a datagrid. I'm bit confused with the solution given to me. Could you please explain
VIPR@T 17-Oct-12 6:52am    
you can save your desired records into one DataTable using this query. after that you can give that DataTable as datasource to gridview.
OleDbCommand ccmd = new OleDbCommand(@"Select LEFT(F2,8) From [SPAT$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(ccmd);
DataTable Data = new DataTable();
adapter.Fill(Data);
for (int y = 16; y < Data.Rows.Count; y++)
{
var value = Data.Rows[y][0].ToString();
}
DataTable Oracle = Data.AsEnumerable().Where((row, index) => index >= 16).CopyToDataTable();
dataGridView1.DataSource = Oracle;
 
Share this answer
 

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