Solved

Import from Excel to datagrid using c#.-- URGENT.

Posted on 2008-06-13
3
2,991 Views
Last Modified: 2010-04-21
hi,

    I have a datagrid with about 10 columns in it.Now i want to import the values in excel file to datagrid.

scenario :

1-----There are only 5 columns in excel.So i want to match the column names for excel and datagrid for populating the same column values in datagrid.
2-----I have a session value session["RiskID"]. I want to check this value against all the values in first column"RiskId" in excel. If any one value in the column dosen't match, then it shud not import to datagrid.Instead it shud prompt a message to user.
3-----I have a column called "SI" in excel. I want to check all the values in this column. If any one value in the column has "0", then it shud not import to datagrid.Instead it shud prompt a message to user.
4-----If any values in excel has values like " Microsoft's " with an apostaphe, it shud not be a problem in importing the values to datagrid.

Please help me on this. Thanks. Its very Urgent
0
Comment
Question by:srk1982
3 Comments
 
LVL 1

Accepted Solution

by:
ron10023 earned 250 total points
ID: 21776730
Hi srk1982,

First of all, you should never use signs like apostaphe in excel because the structure of an excel file is basically xml and we all know that xml doesn't handle those signs well.

After that said, just go to www.connectionstrings.com and take a connection string for excel. This way you can use the excel file just like a database and use an sql query for all your needs.

Hope it helped

Ron
0
 
LVL 14

Assisted Solution

by:Dustin Hopkins
Dustin Hopkins earned 250 total points
ID: 21776945
Ok lets take a swing...
1---Just set your datagrid not to load the columns automatically, instead add them as bound columns and set the datafield to the column name in excel.
2---not completely sure what you mean by not loading, you may want to change your select statement to filter your results or you can loop throught he datagrid and change/edit text.
4---There shouldn't be a problem with apostophes, though it is not prefered usage in excel documents

'Code to query excel
Dim sExcelFileName As String = "~\current.xls"
        Dim sWorkbook As String = "[DSRO$]"
        Dim sExcelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath(sExcelFileName) & ";Extended Properties=""Excel 8.0;HDR=YES;"""
        Dim OleDbConn As OleDbConnection = New OleDbConnection(sExcelConnectionString)
        Dim OleDbCmd As OleDbCommand = New OleDbCommand(("SELECT * FROM " & sWorkbook), OleDbConn)
        OleDbConn.Open()
        Dim dr As OleDbDataReader = OleDbCmd.ExecuteReader()
        dg.DataSource = dr
        dg.DataBind()
        OleDbConn.Close()

Open in new window

0
 

Author Closing Comment

by:srk1982
ID: 31466847
Your post helped me to do the task much faster.Anyway i did this in c#, not VB.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question