复制gridView行

时间:2013-02-18 17:17:33

标签: winforms gridview devexpress

如何将(devexpress)gridView行以另一种形式复制到另一个gridControl? 请写一个例子。这是儿童形式。

public partial class frmSelectInvoice : DevExpress.XtraEditors.XtraForm{

public ValinorEntities valinor;
public BindingSource src;

public frmSelectInvoice()
{
    InitializeComponent();

    using (this.valinor = new ValinorEntities())
    {
        this.valinor = new ValinorEntities();
        this.src = new BindingSource(valinor.invoices_head, null);
        gridControl1.DataSource = src;
        src.DataSource = valinor.invoices_head;
    }
}

private void button1_Click(object sender, EventArgs e)
{

    this.Close();
}

}

2 个答案:

答案 0 :(得分:2)

我也遭受了同样的痛苦。最终我能够开发出一种解决方案,它可以毫无问题地运行。我提供了构建自己的解决方案的基本代码。您可以分两步完成目标。

步骤1:将网格视图行复制到剪贴板[按ctrl + c]

步骤2:将剪贴板信息粘贴到另一个(或相同的)网格视图[按ctrl + v]

第1步:示例代码 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

private void gridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.C)
        {
            Clipboard.SetText(GetSelectedValues(gridView1));
            e.Handled = true;
        }
    }

    private string GetSelectedValues(DevExpress.XtraGrid.Views.Grid.GridView view)
    {
        //validate selected row count
        if (view.SelectedRowsCount == 0) return "";

        const string CellDelimiter = "\t"; 
        const string LineDelimiter = "\r\n";
        string result = "";

        //Iterate cells and compose a tab delimited string of cell values
        for (int i = view.SelectedRowsCount - 1; i >= 0; i--)
        {
            int row = view.GetSelectedRows()[i];
            for (int j = 0; j < view.VisibleColumns.Count; j++)
            {
                result += view.GetRowCellDisplayText(row, view.VisibleColumns[j]);
                if (j != view.VisibleColumns.Count - 1)
                    result += CellDelimiter;
            }

            if (i != 0)
                result += LineDelimiter;
        }
        return result;
    }

*现在,您选择的行将被复制到剪贴板中。因此,如果需要,您可以将这些信息复制到excel或记事本。提示:Ms.Excel总是将内容格式化为比记事本更智能的内容。

第2步:示例代码 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//for retrieving clipboard data
    string ClipboardData {
        get {
            IDataObject iData = Clipboard.GetDataObject();
            if(iData == null) return "";

            if(iData.GetDataPresent(DataFormats.Text))
                return (string)iData.GetData(DataFormats.Text);
            return "";
        }
        set { Clipboard.SetDataObject(value); }
    }

    DataTable tbl;
    private void Form1_Load(object sender, System.EventArgs e) {
        //create a datatable as the data-source for grid-control
        tbl = new DataTable();
        for(int i = 0; i < 3; i++)
            tbl.Columns.Add("Column" + i.ToString());
        for(int i = 0; i < 4; i++)
            tbl.Rows.Add(new object[] { "Item" + i.ToString(), i, 3 - i });
        gridControl1.DataSource = tbl;
    }

    //copy clip board infromation into the grid-view
    private void simpleButton1_Click(object sender, System.EventArgs e) {
        string[] data = ClipboardData.Split('\n');
        if(data.Length < 1) return;
        foreach(string row in data) {
            AddRow(row);
        }
    }

    void AddRow(string data) {
        if(data == string.Empty) return;
        string[] rowData = data.Split(new char[] { '\r', '\x09' });
        DataRow newRow = tbl.NewRow();
        for(int i = 0; i < rowData.Length; i++) {
            if(i >= tbl.Columns.Count) break;
            newRow[i] = rowData[i];
        }
        tbl.Rows.Add(newRow);
    }

附加代码:(如果需要) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

如果要提供选择多行的功能,请使用“MultiSelect”选项,如下例所示。

 public fromLoad()
    {
        InitializeComponent();
        gridView1.OptionsSelection.MultiSelect = true;
        gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
        gridView1.OptionsBehavior.CopyToClipboardWithColumnHeaders = true;
    }

有关CopyToClipboard方法

的更多详细信息,请参阅以下链接

https://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsBaseBaseView_CopyToClipboardtopic

的问候。 Kushan Randima

答案 1 :(得分:0)

这是正确的代码:

for (int i = 0; i < gridView2.RowCount; i++)
{
  gridView3.AddNewRow();              

  for (int ii = 0; ii < gridView3.Columns.Count; ii++)
  {
    gridView3.SetRowCellValue(gridView3.FocusedRowHandle, gridView3.Columns[ii], gridView2.GetRowCellValue(i, gridView2.Columns[ii]));
  }          
}

如何以其他形式将其复制到另一个gridControl?