Copy Paste Di Datagrid
Copy Paste Di Datagrid
Copy Paste Di Datagrid
OleDbCommand Dim dts As DataSet Dim excel As String Dim OpenFileDialog As New
OpenFileDialog OpenFileDialog.InitialDirectory =
My.Computer.FileSystem.SpecialDirectories.MyDocuments OpenFileDialog.Filter = "All Files
(*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls" If
(OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then Dim fi As New
FileInfo(OpenFileDialog.FileName) Dim FileName As String = OpenFileDialog.FileName excel =
fi.FullName conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
excel + ";Extended Properties=Excel 12.0;") dta = New OleDbDataAdapter("Select * From
[Sheet1$]", conn) dts = New DataSet dta.Fill(dts, "[Sheet1$]") DataGridView1.DataSource = dts
DataGridView1.DataMember = "[Sheet1$]" conn.Close()
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
End Sub
End Class
Hello,
Get
DataGridView1.AllowUserToAddRows = False
Dim ClipboardData As IDataObject = Clipboard.GetDataObject()
cmdRun.Enabled = False
If Not ClipboardData Is Nothing Then
If
(ClipboardData.GetDataPresent(DataFormats.CommaSeparatedValue)) Then
cmdRun.Enabled = True
End If
End If
End Sub
Private Sub cmdRun_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdRun.Click
DataGridView1.DataSource = Nothing
Try
Dim ClipboardData As IDataObject = Clipboard.GetDataObject()
CType(ClipboardData.GetData(DataFormats.CommaSeparatedValue), IO.Stream))
FormattedData = ClipboardStream.ReadLine()
SingleRowData = FormattedData.Split(",".ToCharArray)
LoopCounter = 0
Table.Rows.Add(rowNew)
rowNew = Nothing
End While
ClipboardStream.Close()
DataGridView1.DataSource = Table
Else
MessageBox.Show("Clipboard data does not seem to be copied
from Excel!")
End If
Else
MessageBox.Show("Clipboard is empty!")
End If
Catch exp As Exception
MessageBox.Show(exp.Message)
End Try
End Sub
Private Sub cmdCopyRowToClipboard_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdCopyRowToClipboard.Click
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells.Cast(Of
DataGridViewCell)() _
Select cell.Value).ToArray, Function(o) o.ToString)))
' Assuming the data just set is there we now split by comma as we
just sent the
' data comma delimited we now split by comma into a string array
then for displaying
' the data back we place each element into a StringBuilder
object.
Dim ReturningData As String() =
Clipboard.GetText.Split(",".ToCharArray)
Dim sb As New System.Text.StringBuilder
For Each item In ReturningData
sb.AppendLine(item)
Next
MessageBox.Show(sb.ToString)
End If
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no
help, this will help others who are looking for solutions to the same or similar problem.
Hello Everyone Good Afternoon. I have a Program in VB.Net that Exports Data in
Datagridview into an Excel File like this
After Exporting it, I will Edit the Excel File and Return it into my Datagridview but sad to say
here is my output to that
As what you see on both pictures they are Different. No Commas,No Decimal Places and
the 0 in Column Total is also added but Supposed to be it is not.
My Questions is How can I achieve the same format in excel? Put Commas and Decimal
Point in Number Columns and Do not Include the Rows that has a 0 or 0.00 in
Column Total
All I want is that my Datagridview Data is also same as the Format in Excel.
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files
(*.xls)|*xls"
excel = fi.FullName
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel +
";Extended Properties=Excel 12.0;")
dta = New OleDbDataAdapter("Select * From [Sheet1$]", conn)
dts = New DataSet
dta.Fill(dts, "[Sheet1$]")
DataGridView1.DataSource = dts
DataGridView1.DataMember = "[Sheet1$]"
conn.Close()
With DataGridView1
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Item Code"
.Columns(1).HeaderCell.Value = "Description"
.Columns(2).HeaderCell.Value = "Delivery Date"
.Columns(3).HeaderCell.Value = "Stock On-Hand"
.Columns(4).HeaderCell.Value = "Ordering Level"
.Columns(5).HeaderCell.Value = "Order Qty"
.Columns(6).HeaderCell.Value = "Approved Qty"
.Columns(7).HeaderCell.Value = "UOM"
.Columns(8).HeaderCell.Value = "Unit Price"
.Columns(9).HeaderCell.Value = "Total"
.Columns(10).HeaderCell.Value = "Remarks"
====
datagrid.AllowUserToAddRows = False
For rowLine As Integer = 0 To rowLines.Length - 1
End Sub
The following sample shows how to
use GemBox.Spreadsheet.WindowsFormUtilities.dll to import or export ExcelFile to
the Windows.Forms DataGridView control in C# and VB.NET.
Note that not only data is imported or exported from ExcelWorksheet into a DataGridView control
but also hyperlinks, formatting, images, and more.
Input file: WinFormsUtilitiesSample.xlsx
Screenshot
C#
VB.NET
1Imports GemBox.Spreadsheet
2Imports GemBox.Spreadsheet.WinFormsUtilities
3
4Public Class Form1
5
6 Public Sub New()
7 SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
8
9 InitializeComponent()
10 End Sub
11
When press the right button, first thing is to find the selected row
index and then show the contextMenuStrip. From the event
CellMouseUp of dataGridView to indentify the row index and show
the menu item. A global variable rowIndex is assigned the row
index value for later delete this row.
For i As Integer = 1 To 10
dt.Rows.Add(i, "PubName" & i, "Book" & i)
Next
DataGridView1.DataSource = dt
Me.DataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque
Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
Color.Beige
End Sub
DataGridView1.Sort(DataGridView1.Columns(1), ListSortDirection.Ascending)
In the above vb.net code , datagridview sort the title column.
Dim dv As DataView
dv = New DataView(ds.Tables(0), "type = 'business' ", "type Desc",
DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
In the above vb.net code, datagridview is filter the column Type and
the column value is Business.
Imports System.Data.SqlClient
Imports System.ComponentModel
Public Class Form1
Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim connectionString As String = "Data Source=.;Initial
Catalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT title_id,title,type,pub_id FROM Titles"
Dim connection As New SqlConnection(connectionString)
Dim dataadapter As New SqlDataAdapter(sql, connection)
connection.Open()
dataadapter.Fill(ds, "Titles_table")
connection.Close()
DataGridView1.DataSource = ds.Tables(0)
End Sub
Sub NORAC()
Sub Periksa()
Result:
First add a reference to Microsoft Excel 12.0 Object Library
In your project menu click on Project - Add Reference - go to COM tab
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
End Sub
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j,
i).Value.ToString()
Next
Next
xlWorkBook.SaveAs("c:\vb.net-informations.xls",
Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
_
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue,
misValue, misValue)
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
MessageBox.Show("Over")
End Sub
End Class