我正在研究C#windows應用程序,並且希望在DataGridView中複製一行並將其粘貼到新行中。我如何實現這一目標?我正在使用.net框架3.5。DataGridView中的行復制/粘貼功能(Windows應用程序)
你能否給我提供一些想法或一些代碼,以表明我如何實現這一目標?
我正在研究C#windows應用程序,並且希望在DataGridView中複製一行並將其粘貼到新行中。我如何實現這一目標?我正在使用.net框架3.5。DataGridView中的行復制/粘貼功能(Windows應用程序)
你能否給我提供一些想法或一些代碼,以表明我如何實現這一目標?
DataGridViewRow類有一個.Clone方法,它將克隆它所保存的當前行。
有更多的信息來看看here
我已經找到了post包含代碼從剪貼板中值粘貼到DataGridView的。
我google搜索如何粘貼到 的DataGridView在C#中從剪貼板中, 信息,從Excel中複製,並沒有發現 全面的答案。收集來自論壇的 線程,並想出了 這個答案,希望這會讓 有些人的生活更輕鬆。你不必 理解代碼只是複製和粘貼
下面是有點修改後的版本。除了小的重構之外,我禁止將其粘貼到ReadOnly單元格中。
用例:
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
ClipboardUtils.OnDataGridViewPaste(sender, e);
}
代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Commons
{
public class ClipboardUtils
{
public static void OnDataGridViewPaste(object grid, KeyEventArgs e)
{
if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V))
{
PasteTSV((DataGridView)grid);
}
}
public static void PasteTSV(DataGridView grid)
{
char[] rowSplitter = { '\r', '\n' };
char[] columnSplitter = { '\t' };
// Get the text from clipboard
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
// Split it into lines
string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
// Get the row and column of selected cell in grid
int r = grid.SelectedCells[0].RowIndex;
int c = grid.SelectedCells[0].ColumnIndex;
// Add rows into grid to fit clipboard lines
if (grid.Rows.Count < (r + rowsInClipboard.Length))
{
grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count);
}
// Loop through the lines, split them into cells and place the values in the corresponding cell.
for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
{
// Split row into cell values
string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
// Cycle through cell values
for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
{
// Assign cell value, only if it within columns of the grid
if (grid.ColumnCount - 1 >= c + iCol)
{
DataGridViewCell cell = grid.Rows[r + iRow].Cells[c + iCol];
if (!cell.ReadOnly)
{
cell.Value = valuesInRow[iCol];
}
}
}
}
}
}
}
不錯的代碼。沒有準備好黃金時段,但大部分的路上。我在一個個人項目中使用它,只要你先慢慢地按下ctrl + v中的'v',並且網格必須在EditOnKeystrokeOrF2的EditMode中 – Mario 2012-12-04 16:11:40
複製一行到另一個?爲什麼根本不買賬這樣
public DataGridViewRow CloneWithValues(DataGridViewRow row)
{
DataGridViewRow clonedRow = (DataGridViewRow)row.Clone();
for (Int32 index = 0; index < row.Cells.Count; index++)
{
clonedRow.Cells[index].Value = row.Cells[index].Value;
}
return clonedRow;
}
你還可以參考:http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html
希望這有助於! :}
下面是我alex2k8回答修改後的版本,這將用於綁定datagridview的工作。
這裏是未修改的版本
' Add rows into grid to fit clipboard lines
if (grid.Rows.Count < (r + rowsInClipboard.Length))
{
grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count);
}
這裏是我的修改發生希望有人來到這裏幫助dt爲一個DataTable
' Add rows into grid to fit clipboard lines
If grid.Rows.Count < (r + rowsInClipboard.Length) Then
Dim workRow As DataRow
Dim i As Integer
For i = 0 To (r + rowsInClipboard.Length - grid.Rows.Count)
workRow = dt.NewRow()
workRow(0) = ""
dt.Rows.Add(workRow)
Next
End If
注:該代碼是在vb.net使用http://converter.telerik.com/將其轉換回c#
我檢查了它。但在複製數據後,我將其粘貼到gridview的新行。它不會粘貼上一行的數據,而當我在單個文本框中粘貼時發生。 我只是在DataGridView的函數後面複製了粘貼msdn代碼的函數。 – Programmer 2010-01-19 12:41:16