winform中如何实现复制datagridview中的一行粘贴到最后一行,自动再添加一行空行,以便下次粘贴 RT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面代码是我的datagridview的复制代码,这个选择行是fullselect //装载不重复的选中行号 ArrayList arr = new ArrayList(); for (int i = 0; i < grd.SelectedCells.Count; i++) { bool flag = true; for (int j = 0; j < arr.Count; j++) { if (arr[j].ToString().Equals(grd.SelectedCells[i].RowIndex.ToString())) { flag = false; break; } } if (flag) { arr.Add(grd.SelectedCells[i].RowIndex.ToString()); } } //判断行号是顺序的还是倒序的,然后复制 if (clsC.Cint(arr[0].ToString()) > clsC.Cint(arr[1].ToString())) { for (int i = arr.Count - 1; i >= 0; i--) { int hang = grd.RowCount - 1; grd.RowCount = grd.RowCount + 1; grd.Rows[hang].Cells[0].Value = (clsC.Cint(grd.Rows[(hang - 1)].Cells[0].Value.ToString()) + 1).ToString(); grd.Rows[hang].Cells[1].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[1].Value.ToString(); grd.Rows[hang].Cells[2].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[2].Value.ToString(); grd.Rows[hang].Cells[3].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[3].Value.ToString(); grd.Rows[hang].Cells[4].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[4].Value.ToString(); if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value == null) { grd.Rows[hang].Cells[5].Value = ""; } else { grd.Rows[hang].Cells[5].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value.ToString(); } if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value == null) { grd.Rows[hang].Cells[6].Value = ""; } else { grd.Rows[hang].Cells[6].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value.ToString(); } } } else { for (int i = 0; i < arr.Count; i++) { int hang = grd.RowCount - 1; grd.RowCount = grd.RowCount + 1; grd.Rows[hang].Cells[0].Value = (clsC.Cint(grd.Rows[(hang - 1)].Cells[0].Value.ToString()) + 1).ToString(); grd.Rows[hang].Cells[1].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[1].Value.ToString(); grd.Rows[hang].Cells[2].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[2].Value.ToString(); grd.Rows[hang].Cells[3].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[3].Value.ToString(); grd.Rows[hang].Cells[4].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[4].Value.ToString(); if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value == null) { grd.Rows[hang].Cells[5].Value = ""; } else { grd.Rows[hang].Cells[5].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value.ToString(); } if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value == null) { grd.Rows[hang].Cells[6].Value = ""; } else { grd.Rows[hang].Cells[6].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value.ToString(); } } } 利用剪贴板功能就能复制了吧?//copy public void DataGridViewEnableCopy(DataGridView p_Data) { Clipboard.SetData(DataFormats.Text, p_Data.GetClipboardContent()); }只是粘贴的时候public void DataGirdViewCellPaste(DataGridView p_Data) { //try //{ //this.dataGridView1.BeginEdit(true); //this.dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically; // 获取剪切板的内容,并按行分割 string pasteText = Clipboard.GetText().Replace ('\t',' ').Remove (0,1); if (string.IsNullOrEmpty(pasteText)) return; string[] lines = pasteText.Split(new char[] {' ', ' ' }); DataGridViewRow dr = p_Data.Rows[p_Data.NewRowIndex]; //p_Data.Rows.Add(dr); for (int i = 0; i < p_Data.Columns.Count; i++) { dr.Cells[i].Value = lines[i]; } p_Data.Rows.Add(dr); dataGridView1.AllowUserToAddRows = true ; //this.dataGridView1.EndEdit(); //} }总是提示我“当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行”,不知如何是好? 这个报错是因为你的DataGridView是绑定的数据源,所以无法添加新行,你想要添加新行的话,你应该在最开始放数据到DataGridView的时候用代码放上去,而不是绑定数据源!我上面的方法是可以复制多行的代码 C# Xml 自定义序列化格式(将实体类格式转换为固定格式的XML) winform中的treeview控件怎样绑定数据库 做了一个系统运行起来,左下角老是提示“网页上有错误”! 关于加密密匙保存 CS1519: 类、结构或接口成员声明中的标记“=”无效 要怎么改正? 小弟是.NET的初学者.提个小小的问题. 关于sidebar的一些问题 请用过帮助精灵的指点一下! 有关正则表达式 急求c# 与三菱plc 以太网通讯 案例 源代码 Ajax UpdatePanel中 如何弹出确认对话框 如何以字符串动态调用函数?
ArrayList arr = new ArrayList();
for (int i = 0; i < grd.SelectedCells.Count; i++)
{
bool flag = true;
for (int j = 0; j < arr.Count; j++)
{
if (arr[j].ToString().Equals(grd.SelectedCells[i].RowIndex.ToString()))
{
flag = false;
break;
}
} if (flag)
{
arr.Add(grd.SelectedCells[i].RowIndex.ToString());
}
} //判断行号是顺序的还是倒序的,然后复制
if (clsC.Cint(arr[0].ToString()) > clsC.Cint(arr[1].ToString()))
{
for (int i = arr.Count - 1; i >= 0; i--)
{
int hang = grd.RowCount - 1;
grd.RowCount = grd.RowCount + 1;
grd.Rows[hang].Cells[0].Value = (clsC.Cint(grd.Rows[(hang - 1)].Cells[0].Value.ToString()) + 1).ToString();
grd.Rows[hang].Cells[1].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[1].Value.ToString();
grd.Rows[hang].Cells[2].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[2].Value.ToString();
grd.Rows[hang].Cells[3].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[3].Value.ToString();
grd.Rows[hang].Cells[4].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[4].Value.ToString();
if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value == null)
{
grd.Rows[hang].Cells[5].Value = "";
}
else
{
grd.Rows[hang].Cells[5].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value.ToString();
} if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value == null)
{
grd.Rows[hang].Cells[6].Value = "";
}
else
{
grd.Rows[hang].Cells[6].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value.ToString();
}
}
}
else
{
for (int i = 0; i < arr.Count; i++)
{
int hang = grd.RowCount - 1;
grd.RowCount = grd.RowCount + 1;
grd.Rows[hang].Cells[0].Value = (clsC.Cint(grd.Rows[(hang - 1)].Cells[0].Value.ToString()) + 1).ToString();
grd.Rows[hang].Cells[1].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[1].Value.ToString();
grd.Rows[hang].Cells[2].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[2].Value.ToString();
grd.Rows[hang].Cells[3].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[3].Value.ToString();
grd.Rows[hang].Cells[4].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[4].Value.ToString();
if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value == null)
{
grd.Rows[hang].Cells[5].Value = "";
}
else
{
grd.Rows[hang].Cells[5].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[5].Value.ToString();
} if (grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value == null)
{
grd.Rows[hang].Cells[6].Value = "";
}
else
{
grd.Rows[hang].Cells[6].Value = grd.Rows[clsC.Cint(arr[i].ToString())].Cells[6].Value.ToString();
}
}
}
利用剪贴板功能就能复制了吧?
//copy
public void DataGridViewEnableCopy(DataGridView p_Data)
{
Clipboard.SetData(DataFormats.Text, p_Data.GetClipboardContent());
}
只是粘贴的时候
public void DataGirdViewCellPaste(DataGridView p_Data)
{
//try
//{
//this.dataGridView1.BeginEdit(true);
//this.dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
// 获取剪切板的内容,并按行分割
string pasteText = Clipboard.GetText().Replace ('\t',' ').Remove (0,1);
if (string.IsNullOrEmpty(pasteText))
return;
string[] lines = pasteText.Split(new char[] {' ', ' ' });
DataGridViewRow dr = p_Data.Rows[p_Data.NewRowIndex];
//p_Data.Rows.Add(dr);
for (int i = 0; i < p_Data.Columns.Count; i++)
{
dr.Cells[i].Value = lines[i];
}
p_Data.Rows.Add(dr);
dataGridView1.AllowUserToAddRows = true ;
//this.dataGridView1.EndEdit();
//}
}总是提示我“当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行”,不知如何是好?
这个报错是因为你的DataGridView是绑定的数据源,所以无法添加新行,你想要添加新行的话,你应该在最开始放数据到DataGridView的时候用代码放上去,而不是绑定数据源!我上面的方法是可以复制多行的代码