RT

解决方案 »

  1.   

    下面代码是我的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();
                        }
                    }
                }
      

  2.   


    利用剪贴板功能就能复制了吧?
    //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 的行集合中添加行”,不知如何是好?
      

  3.   


    这个报错是因为你的DataGridView是绑定的数据源,所以无法添加新行,你想要添加新行的话,你应该在最开始放数据到DataGridView的时候用代码放上去,而不是绑定数据源!我上面的方法是可以复制多行的代码