在DGV中合并Excel数据集 [英] Merging excel datasets inti a DGV
本文介绍了在DGV中合并Excel数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一次机会从多个来源获取记录.我下面的代码适用于单个选择,它恰好是分配给最后一个数据集的代码.我想将数据集合并到
一个大数据集,以导入到数据网格视图中.
[ ] ^ ]
I am having one heck of a time getting records from multiple sources. The code I have below works on a single selection, and it happens to be the one assigned to the last dataset. I want to merge the datasets into
one large dataset, to import into a data grid view.
[]Read and Write Excel Files[^]
<pre lang="vb">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Dim i As Integer
For i = 1 To 4
If Not String.IsNullOrEmpty(txtFileName1.Text) Then
Panel1.Visible = False
btnClose.Enabled = False
Dim OExcelHandler1 As New ExcelHandler()
Dim ds1 As DataSet = OExcelHandler1.GetDataFromExcel(txtFileName1.Text.Trim())
If ds1 IsNot Nothing Then
dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvExcelData.DataSource = DataGridViewEditMode.EditProgrammatically
dgvExcelData.DataSource = ds1.Tables(0)
End If
End If
Next
For i = 2 To 4
If Not String.IsNullOrEmpty(txtFileName2.Text) Then
Panel1.Visible = False
btnClose.Enabled = False
Dim OExcelHandler2 As New ExcelHandler()
Dim ds2 As DataSet = OExcelHandler2.GetDataFromExcel(txtFileName2.Text.Trim())
If ds2 IsNot Nothing Then
dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvExcelData.DataSource = DataGridViewEditMode.EditProgrammatically
dgvExcelData.DataSource = ds2.Tables(0)
End If
End If
Next
For i = 3 To 4
If Not String.IsNullOrEmpty(txtFileName3.Text) Then
Panel1.Visible = False
btnClose.Enabled = False
Dim OExcelHandler3 As New ExcelHandler()
Dim ds3 As DataSet = OExcelHandler3.GetDataFromExcel(txtFileName3.Text.Trim())
If ds3 IsNot Nothing Then
dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvExcelData.DataSource = DataGridViewEditMode.EditProgrammatically
dgvExcelData.DataSource = ds3.Tables(0)
End If
End If
Next
If Not String.IsNullOrEmpty(txtFileName4.Text) Then
Panel1.Visible = False
btnClose.Enabled = False
Dim OExcelHandler4 As New ExcelHandler()
Dim ds4 As DataSet = OExcelHandler4.GetDataFromExcel(txtFileName4.Text.Trim())
If ds4 IsNot Nothing Then
dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvExcelData.DataSource = DataGridViewEditMode.EditProgrammatically
dgvExcelData.DataSource = ds4.Tables(0)
End If
End If
''Catch ex As Exception
btnClose.Enabled = True
End Sub
推荐答案
private void BindData(string StrFileName)
{
OleDbConnection objConn = new OleDbConnection();
OleDbCommand objCmd = new OleDbCommand();
OleDbDataAdapter dtAdapter = new OleDbDataAdapter();
DataSet ds = new DataSet();
String strConnString, strSQL;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("CSVFile/") +
";Extended Properties='TEXT;HDR=Yes;FMT=Delimited;Format=Delimited(,)'";
strSQL = "SELECT * FROM " + StrFileName;
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
//*** BindData to GridView ***//
gr_info.DataSource = ds.Tables[0];
grv_info.DataBind();
dtAdapter = null;
objConn.Close();
objConn = null;
}
谢谢
Mahesh Patel
vipsha16@yahoo.com
Thanks
Mahesh Patel
vipsha16@yahoo.com
这篇关于在DGV中合并Excel数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文