Datagrid|xml The following source code is given in the Dbf,xls,xml,mdb file into the C#datagrid method, for you to reference.
PutInDataSet.cs's source code
Using System;
Using System.Data.Odbc;
Using System.Data.OleDb;
Using System.Data;
Using System.Collections;
Namespace Putindataset
{
<summary>
Summary description of the Datasettransin.
</summary>
public class Putindataset
{
<summary>
File variables passed in
</summary>
Private dataset my_ds;//data set for storing files
private string my_err;//error message
private string my_tablename;//The file name passed in
Private tabletype my_tabletype;//Incoming file type
private string my_tablepath;//incoming file path
Index of private int my_tableindex;//table
OleDbCommandBuilder my_builder;//Command string
<summary>
Types of files that can be processed
</summary>
public enum Tabletype
{
Mdb,xls,dbf,doc,txt,xml,html
}
Public Putindataset (String tablepath,string tablename,tabletype tabletype)
{
<summary>
Get the incoming path, filename and file type;
</summary>
this.my_tablepath=tablepath;//Path
this.my_tablename=tablename;//file name
this.my_tabletype=tabletype;//file type
}
Public DataSet Convert ()
{
DataSet irtn_ds=new DataSet ();
Switch (this.my_tabletype)
{
Case TABLETYPE.DBF:
Irtn_ds = this. Dbftods ();
Break
Case Tabletype.mdb:
Irtn_ds = this. Mdbtods ();
Break
Case Tabletype.xls:
Irtn_ds = this. Xlstods ();
Break
<summary>
Put an Excel file into a dataset
</summary>
Private DataSet Xlstods ()
{
OleDbConnection My_conn;
OleDbDataAdapter my_adapter;
Database connection
This.my_strconnection= "Provider=Microsoft.Jet.OLEDB.4.0; Extended properties=excel 8.0;data source= "+this.my_tablepath+this.my_tablename;
This.my_strselect= "SELECT * from [sheet1$]";
My_conn = new OleDbConnection (this.my_strconnection);
My_conn. Open ();
My_adapter = new OleDbDataAdapter (this.my_strselect,my_conn);
This.my_builder=new OleDbCommandBuilder (My_adapter);
This.my_ds=new DataSet ();
Populating data sets
My_adapter.fill (This.my_ds, "exceldata");
return this.my_ds;
}
}
}
Form_putindataset.cs's source code
Using System;
Using System.Data;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using Dataaccess.sysmanage;
Using BusinessRules;
Using Datasettrans;
Namespace Winform.common
{
<summary>
Summary description of the formdesktop.
</summary>
public class FormDesktop:System.Windows.Forms.Form
{
Private WinForm.Common.DeskTop DeskTop1;
Private System.Windows.Forms.Button button1;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Private DataSet M_ds = new DataSet ();
Private System.Windows.Forms.DataGrid dataGrid1; Data source
private string M_tablename; External file name
Public Formdesktop ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
#region Windows Form Designer generated code
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
THIS.DESKTOP1 = new WinForm.Common.DeskTop ();
This.button1 = new System.Windows.Forms.Button ();
THIS.DATAGRID1 = new System.Windows.Forms.DataGrid ();
((System.ComponentModel.ISupportInitialize) (THIS.DATAGRID1)). BeginInit ();
This. SuspendLayout ();
//
DeskTop1
//
This.deskTop1.BackColor = System.Drawing.SystemColors.Desktop;
This.deskTop1.Location = new System.Drawing.Point (168, 440);
This.deskTop1.Name = "DESKTOP1";
This.deskTop1.Size = new System.Drawing.Size (16, 24);
This.deskTop1.TabIndex = 0;
//
Button1
//
This.button1.Location = new System.Drawing.Point (256, 216);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (88, 40);
This.button1.TabIndex = 1;
This.button1.Text = "GetData";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
DataGrid1
//
This.dataGrid1.DataMember = "";
This.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
This.dataGrid1.Location = new System.Drawing.Point (192, 40);
This.dataGrid1.Name = "DATAGRID1";
This.dataGrid1.Size = new System.Drawing.Size (216, 152);
This.dataGrid1.TabIndex = 2;
//
Formdesktop
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. BackColor = System.Drawing.SystemColors.AppWorkspace;
This. ClientSize = new System.Drawing.Size (624, 485);
This. Controls.Add (THIS.DATAGRID1);
This. Controls.Add (This.button1);
This. Controls.Add (THIS.DESKTOP1);
This. Name = "Formdesktop";
This. Text = "System console";
This. Resize + = new System.EventHandler (this. Formdesktop_resize);
This. Load + = new System.EventHandler (this. Formdesktop_load);
((System.ComponentModel.ISupportInitialize) (THIS.DATAGRID1)). EndInit ();
This. ResumeLayout (FALSE);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.