By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,672 Members | 1,628 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

View a DBF file in DataGridView using C#

P: 2
Hi all,

I am trying to use datagridview to view a dbf file. It is a single dbf file and is not a part of any database. How do I do this in Visual Studio 2005 using C#.

Could you help me my providing me with a code to use for this purpose?

Your help in this matter is highly appreciated.

Thanking you in advance.

Regards,
Deepak
Aug 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: 1
Expand|Select|Wrap|Line Numbers
  1. using System.Data.OleDb; 
  2.         private void ImportDBF_Load(object sender, EventArgs e) 
  3.         { 
  4.             if (ofdDBF.ShowDialog()==DialogResult.OK) 
  5.             { 
  6.                 string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ofdDBF.FileName.Substring(0, ofdDBF.FileName.LastIndexOf("\\")) + ";Extended Properties=dBASE IV;"; 
  7.  
  8.                 OleDbConnection conn = new OleDbConnection(connStr); 
  9.                 conn.Open(); 
  10.  
  11.                 string cmd_string = "select * from " + ofdDBF.SafeFileName.Substring(0, ofdDBF.SafeFileName.IndexOf(".")); 
  12.                 MessageBox.Show(cmd_string); 
  13.                 OleDbDataAdapter da = new OleDbDataAdapter(cmd_string, conn); 
  14.                 DataSet ds = new DataSet(); 
  15.                 da.Fill(ds); 
  16.                 dgvImport.DataSource = ds.Tables[0]; 
  17.  
  18.             } 
  19.  
  20.         }

I hope it's useful .
Feb 5 '09 #2

Frinavale
Expert Mod 5K+
P: 9,731
The code above retrieves the data from the DBF file and loads it into memory...then it uses this as the data source for the DataGridView's.

For more information on how to use a database in your programs please see how to use a database in your program. Also, take a look at how to use a database in your program part II for more information on how to connect to databases that are not MSSql.
Feb 5 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.