Click here to Skip to main content
15,127,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Posted

1 solution

C++
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;

    }



Thanks
Mahesh Patel
vipsha16@yahoo.com
   

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