簡體   English   中英

將多個選定的行從一個網格復制到另一個網格

[英]Copy multiple selected rows from one grid to another

我有這段代碼將選定的行從一個網格復制到另一個網格

private void btnAddEmployee_Click(object sender, EventArgs e)
{
    LayoutControl lc = new LayoutControl();
    lc.Dock = DockStyle.Top;
    LookUpEdit userShift = new LookUpEdit();
    userShift.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
    userShift.Properties.DataSource = paint.GetShiftTime();
    userShift.Properties.DisplayMember = "ShiftTime";
    userShift.Properties.ValueMember = "id";
    userShift.Properties.ShowHeader = false;
    var date = DateTime.Now;

    if (8 < date.Hour && date.Hour < 16)
    {
        userShift.EditValue = 1;
    }
    else if (16 < date.Hour && date.Hour < 24)
    {
        userShift.EditValue = 2;
    }
    else
    {
        userShift.EditValue = 3;
    }

    lc.AddItem(Resources.workingHours, userShift).TextVisible = true;
    lc.Height = 50;
    this.Controls.Add(lc);
    this.Dock = DockStyle.Top;
    int[] selectedRows = gridView4.GetSelectedRows();

    for(int n=0;n< selectedRows.Length;n++)
    //foreach (int index in selectedRows)
    {
        if (DevExpress.XtraEditors.XtraDialog.Show(lc, Resources.options,                         MessageBoxButtons.OKCancel) == DialogResult.OK)
        { 
            //Prevent duplicate data
            for (int i = 0; i < gridView5.RowCount; i++)
            {
                if (gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString() == gridView5.GetRowCellValue(i, "Matricule").ToString())
                {
                    XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            DataRow r = EmplDT.NewRow();
            r[0] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString();
            r[1] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Employé").ToString();
            r[2] = userShift.Text;
            r[3] = userShift.EditValue;
            r[4] = txtDate.EditValue;
            EmplDT.Rows.Add(r);

這是我的代碼在gridview 5中創建列

DataTable EmplDT = new DataTable();
 void CreateEmployeeTable()
    {
        EmplDT.Columns.Add("Matricule");
        EmplDT.Columns.Add("Employé");
        EmplDT.Columns.Add("Heure");
        EmplDT.Columns.Add("idShiftTime", typeof(Int32));
        EmplDT.Columns.Add("Date", typeof(DateTime));
        gridControl5.DataSource = EmplDT;
        gridView5.Columns["idShiftTime"].Visible = false;
        gridView5.Columns["Date"].Visible = false;
    }

我在此代碼中有兩個問題:
第一個是當我運行代碼時,它僅添加第一條記錄,然后出現重復的錯誤消息。
第二個我只想第一次顯示布局控制。
在此先感謝,對不起我的英語。

從devexpress網格視圖中循環遍歷選定的行並獲取行可以像這樣更簡單

int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
     // Get a DataRow and fill it with all values from the this selected row
     // This is where you went wrong, you kept using only the first selected row
     DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;

     // Do a check for doubles here
     DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() +"'");
     if (doubles.Length > 0)
     {
         XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return;         
     }

     // fix for error  "This row already belongs to another table"
     DataRox row = EmplDT.NewRow();
     row[0] = rowGridView4[0];
     row[1] = rowGridView4[1];
     row[2] = userShift.Text;
     row[3] = userShift.EditValue;
     row[4] = txtDate.EditValue;

     EmplDT.Rows.Add(row);
}

請注意,在此位置進行雙打測試將導致所有記錄被復制,直到找到重復為止。 因此,在您的錯誤消息之后,可能會復制一些記錄,而有些則不會。
那是你的打算嗎?

我會忽略錯誤消息,只是跳過重復的記錄。 如果需要,您仍然可以顯示一條消息,其中包含多個記錄。

int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
     // Get a DataRow and fill it with all values from the this selected row
     // This is where you went wrong, you kept using only the first selected row
     DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;

     // Do a check for doubles here
     DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() + "'");
     if (doubles.Length == 0)
     {
         // fix for error  "This row already belongs to another table"
         DataRox row = EmplDT.NewRow();
         row[0] = rowGridView4[0];
         row[1] = rowGridView4[1];
         row[2] = userShift.Text;
         row[3] = userShift.EditValue;
         row[4] = txtDate.EditValue;

         EmplDT.Rows.Add(row);
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM