ぽちグラマー的 .Netに戻る
DataGridViewは、初期設定のままでは行の切り貼りができません。
・コピーした行の漢字が化ける
・エンコーティングにより文字化けを修正した
・DataGridViewには貼り付けの機能は無い(上書き、コピーした行追加)
こんな経験をしたので下記サンプルを作ってみた
サンプルソース
'DataGridViewの行のコピー関数
Private Sub GridToClip()
Dim tmpGrd As DataGridView
Try
tmpGrd = ActiveControl
Catch e As Exception
Exit Sub
End Try
Dim itm As Object
Dim rw As DataGridViewRow = tmpGrd.CurrentRow
Dim sTmp As String = ""
Dim i As Integer = 0
For Each itm In rw.Cells
sTmp += rw.Cells(i).FormattedValue & vbTab
i += 1
Next
sTmp = sTmp.Substring(0, sTmp.Length - 1)
Clipboard.SetDataObject(sTmp, TextDataFormat.Text)
End Sub
'クリップボードの中身をDataGridViewに貼り付ける
Private Sub ClipToGrid()
Dim arStr As Array
Dim tmpGrd As DataGridView
Dim pasteText As String = ""
Dim tmpStr As String = ""
Dim i As Integer = 0
Dim itm As Object
Try
tmpGrd = ActiveControl
Catch e As Exception
Exit Sub
End Try
pasteText = Clipboard.GetText()
If (TypeOf pasteText Is System.String) = False Then Exit Sub
If pasteText.ToString.Trim.Length <= 0 Then Exit Sub
tmpStr = pasteText
arStr = Split(tmpStr, vbTab)
If arStr.Length <> tmpGrd.Columns.Count Then Exit Sub
Dim iCRow As Integer = tmpGrd.CurrentCell.RowIndex
Dim rw As DataGridViewRow = tmpGrd.CurrentRow
If tmpGrd.CurrentRow.IsNewRow = True Then
For Each itm In rw.Cells
rw.Cells(i).Value = arStr(i)
i += 1
Next
Else
tmpGrd.Rows.Insert(iCRow, New DataGridViewRow)
For Each itm In rw.Cells
tmpGrd.Rows(iCRow).Cells(i).Value = arStr(i)
i += 1
Next
End If
End Sub
'DataGridViewの削除
Private Sub GridRowDelete()
Dim tmpGrd As DataGridView
Dim rwidx As Integer = tmpGrd.CurrentRow.Index
Try
tmpGrd = ActiveControl
Catch e As Exception
Exit Sub
End Try
If tmpGrd.CurrentRow.IsNewRow = True Then Exit Sub
tmpGrd.Rows.RemoveAt(rwidx)
End Sub
|
|
中古パソコンとパソコンパーツの通販ショップGENO
[アウトレットと中古パソコン けっこう安い。]のイオシスのオンライン販売
エンジニア・IT技術者募集求人サイト
全国の求人情報が職種や勤務地、雇用形態から検索できる
在宅ビジネス
在宅ビジネス
副業・サイドビジネス・在宅ワークなら【うるぷろ.com】
「PCソフト」「PCゲーム」「電子書籍」「音楽・楽譜」と、多彩なジャンルのダウンロード
データ入力代行及びスキャニングサービス
翻訳に関わる人を応援するメンバーズクラブ
クリエイターのためのポータルサイト
オンライン書店「楽天ブックス」
動画を簡単にコピーして持ち運べる<vRec+ VR100>
|