Good day guys I need help for adding 2 column in datagridview
my project is :
the import and the varibles are :
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Public Class Form2
Friend xlApp As New Excel.Application
Friend xlWorkBook As Excel.Workbook
Friend xlWorkSheet As Excel.Worksheet
Friend ch As String
Friend ExcelSheetName As Object
Friend exWS2 As Microsoft.Office.Interop.Excel.Worksheet
i open any file excel with openfiledialog this is the code:
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
ch = OpenFileDialog1.FileName
Process.Start("Excel", TextBox1.Text)
ComboBox1.Items.Clear()
End Sub
i open any sheet excel this is the code:
Public Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim x1 As String
xlApp.Visible = False
xlWorkBook = xlApp.Workbooks.Open(ch)
xlWorkSheet = CType(xlWorkBook.Sheets(1), Worksheet)
If ComboBox1.Items.Count = Nothing Then
With xlWorkSheet
For Each Me.xlWorkSheet In xlWorkBook.Worksheets
x1 = xlWorkSheet.Name
ComboBox1.Items.Add(x1)
Next xlWorkSheet
End With
Else
MsgBox("Déja Pleine!", CType(MessageBoxIcon.Error, MsgBoxStyle))
End If
End Sub
i open any column excel this is the code:
Public Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
xlWorkSheet = CType(xlWorkBook.Sheets(ComboBox1.Text), Excel.Worksheet)
xlWorkSheet.Activate()
xlApp.Visible = True
Dim key As String = CStr(DirectCast(ComboBox2.SelectedItem, KeyValuePair(Of Integer, String)).Key)
Dim value As String = DirectCast(ComboBox2.SelectedItem, KeyValuePair(Of Integer, String)).Value
Dim DoesSheetExists As Boolean = False
For Each xs In xlApp.Sheets
If xs.Name = value Then
DoesSheetExists = True
End If
Next
If DoesSheetExists = True Then
MsgBox("Sheet already exists", CType(MessageBoxIcon.Error, MsgBoxStyle))
Else
With xlWorkSheet
Dim lastrow As Integer = xlWorkSheet.Cells.Rows.End(XlDirection.xlDown).Row
Dim colletter As String = ColumnIndexToColumnLetter(CInt(key))
exWS2 = DirectCast(xlWorkBook.Sheets.Add, Microsoft.Office.Interop.Excel.Worksheet)
exWS2.Name = value
xlWorkSheet.Range("A1:A" & lastrow.ToString).Copy(exWS2.Range("A1"))
xlWorkSheet.Range(colletter & "1:" & colletter & lastrow.ToString).Copy(exWS2.Range("B1"))
exWS2.Range("A1").Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue)
exWS2.Range("B1").Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue)
exWS2.Range("A1").Interior.ColorIndex = 8
exWS2.Range("B1").Interior.ColorIndex = 8
exWS2.Range("A2:A" & lastrow.ToString).Interior.ColorIndex = 20
exWS2.Range("A1:A" & lastrow.ToString).HorizontalAlignment = -4108
exWS2.Range("B1:B" & lastrow.ToString).HorizontalAlignment = -4108
exWS2.Range("A1").Font.Name = "Times New Roman"
exWS2.Range("B1").Font.Name = "Times New Roman"
exWS2.Range("B1").Font.FontStyle = "Gras"
exWS2.Range("A1").Font.FontStyle = "Gras"
this column will be add in the new sheet which has the name of the column that i select of the combobox .
what i want is to add the column that i opened in datagridview1
Also i have to calculate sum , max and min of the column, I want to add also the sum , max and min in datagridview2
my form that open excel file and add the sheet :[Click Here
And my file excel :Click Here
please help me