NET中没有数据缓冲的概念,实现Insert操作都难,在自定义报表中修改数据行显示顺序更是十分麻烦。本文将介绍如何通过简单的剪切与粘帖来实现数据行顺序的修改。
ShowSequence保存字段的显示顺序,右键菜单提供剪切和粘贴功能,允许用户多选和多次剪切然后一次粘贴,若右键在空白区域,增行显示在最末行以后;否则插入在选择行的前面,并默认选中被插入行的首行。
clickSpace保存右键的位置是空白区域or not。在GridView的MouseUp事件中实现,注意如何判定光标所在位置的做法。
代码段如下:
  private void gridViewStyleSheet_MouseUp(object  sender, MouseEventArgs e)
          {
              DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi =  gridViewStyleSheet.CalcHitInfo(e.Location);
              if  (e.Button == MouseButtons.Right &
                   hi.HitTest == DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitTest.EmptyRow)
              {
                   clickSpace = true;
              }
              else
              {
                   clickSpace = false;
              }
               //MessageBox.Show(clickSpace.ToString());
        }
剪切:用一个DataTable存储移出的数据,然后从gridview中删除记录。注意每次剪切前DataTable必须清空(clear),注意行从一个DataTable复制到另一个DataTable的做法。另外一个做法是用DataRow.ItemArray.由于是地址引用,不可以直接用DataRow赋值。
代码段如下:
 private void iCut_Click(object sender, EventArgs e)
          {
              dt.Clear();
              int[]  selectedRowHandle = gridViewStyleSheet.GetSelectedRows();
              foreach (int  i in selectedRowHandle)
              {
                   dt.ImportRow(gridViewStyleSheet.GetDataRow(i));
              }
               gridViewStyleSheet.DeleteSelectedRows();
        }
粘贴:分为两种情况,一是Insert,二是Apprend。若在行上右键则为Insert,若为空白区域则为Append。由clickSpace界定。Insert时,将focusrow及其以后的行均移入DataTable,变相实现插入功能。
代码段如下:
        private void iPaste_Click(object  sender, EventArgs e)
          {
              int  curRowHandle = gridViewStyleSheet.FocusedRowHandle;
              int rowCount  = gridViewStyleSheet.RowCount;
               if (!clickSpace)
              {
                   //实现插入行功能
                   if (curRowHandle < rowCount)
                   {
                       for (int i = curRowHandle; i <= rowCount; i++)
                       {
                           dt.ImportRow(gridViewStyleSheet.GetDataRow(i));
                           gridViewStyleSheet.SelectRow(i);
                       }
                       gridViewStyleSheet.DeleteSelectedRows();
                   }
              }
              else
              {
                   curRowHandle = gridViewStyleSheet.RowCount;
              }
  
              foreach  (DataRow dataRow in dt.Rows)
              {
                   gridViewStyleSheet.AddNewRow();
                   int rowHandle = gridViewStyleSheet.FocusedRowHandle;
                   DataRow newRow = gridViewStyleSheet.GetDataRow(rowHandle);
                   if (newRow != null)
                   {
                       newRow.ItemArray = dataRow.ItemArray;
                   }
              }
               dt.Clear();
               gridViewStyleSheet.SelectRow(curRowHandle);
               this.Validate();
               styleSheetBindingSource.EndEdit();
          }
保存:保存时按照显示顺序对ShowSequence进行赋值。
代码段如下:
  private void iSave_Click(object sender, EventArgs e)
          {
              try
              {
                   int rowCount = gridViewStyleSheet.RowCount;
                   for (int i = 0; i < rowCount; i++)
                   {
                       gridViewStyleSheet.SetRowCellValue(i, "ShowSequence", i + 1);
                   }
  
                   this.Validate();
                   styleSheetBindingSource.EndEdit();
                   styleBindingSource.EndEdit();
                   styleSheetTableAdapter.Update(dSStyle.StyleSheet);
                   styleTableAdapter.Update(dSStyle.Style);
              }
              catch  (DBConcurrencyException ex)
              {
                   MessageBox.Show(ex.Row[0].ToString() + ex.Message);
              }
              finally
              {
                   //gridViewStyleSheet.OptionsCustomization.AllowSort = true;
              }
        }
至此,修改数据行的显示顺序便实现了。
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/180.html
    
    
   
 联系电话:023-68661681


                        
                        
返回