1

如何以另一种形式将(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();
}

}

4

2 回答 2

3

我正在遭受同样的痛苦。最终,我能够开发出一个解决方案,并且它可以毫无问题地工作。我已经提供了构建您自己的解决方案的基本代码。您可以分两步完成您的目标。

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

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

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

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 总是比记事本更智能地格式化内容。

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

//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

问候。贵霜兰迪玛

于 2014-10-27T10:38:32.360 回答
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?

于 2013-02-19T10:03:39.230 回答