C# implements the operation of quickly deleting and adding rows in Dev

I won't talk about the theory, just look at the purchasing agent directly, and the code is more intuitive

 interface IFormOperator<T>
    {
        void FormDeleteRecord(T gridView);
        void FormAddRecord(T gridView,Dictionary<string,string> dic);
        void FormAddRecord(T gridView);
      
    }
    class FormOperatorGridView : IFormOperator<DevExpress.XtraGrid.Views.Grid.GridView>
    {
        public void FormDeleteRecord(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            List<int> listSelect = new List<int>();
            listSelect.AddRange(gridView.GetSelectedRows());
            for (int i = listSelect.Count-1; i >-1 ; i--)
            {
                gridView.DeleteRow(listSelect[i]);
            }
            if (gridView.RowCount>0)
            {
                if (gridView.RowCount>listSelect[listSelect.Count-1]&&gridView.RowCount>=1)
                {
                    gridView.FocusedRowHandle = listSelect[listSelect.Count - 1];
                    gridView.SelectRow(listSelect[listSelect.Count - 1]);
                }
                else
                {
                    gridView.FocusedRowHandle = listSelect[listSelect.Count - 1]-1;
                    gridView.SelectRow(listSelect[listSelect.Count - 1]-1);
                }    
            }
        }
        public void FormAddRecord(DevExpress.XtraGrid.Views.Grid.GridView gridView, Dictionary<string, string> dic)
        {
            gridView.AddNewRow();
            gridView.BeginInit();


            int handle = gridView.RowCount == 0 ? 0 : gridView.GetRowHandle(gridView.RowCount-1);
            foreach (string keys in dic.Keys)
            {
                gridView.SetRowCellValue(handle, keys, dic[keys]);
            }
            gridView.EndInit();
            gridView.UpdateCurrentRow();


            handle = gridView.GetRowHandle(gridView.RowCount - 1);
            gridView.FocusedRowHandle = handle;
            gridView.SelectRow(handle);
        }


        public void FormAddRecord(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            gridView.AddNewRow();
            gridView.BeginInit();


            int handle = gridView.RowCount==0?0:gridView.GetRowHandle(gridView.RowCount - 1);
            for (int i = 0; i < gridView.RowCount; i++)
            {
                gridView.SetRowCellValue(handle, gridView.Columns[i].Caption, string.Format("新建{0}行", gridView.Columns[i].Caption, i));
            }
        }
    }
    ///add by hgl 2017-10-27
    /// <summary>
    /// DataGridView操作封装
    /// </summary>
    class FormOperatorDataGridView : IFormOperator<System.Windows.Forms.DataGridView>
    {


        public void FormDeleteRecord(System.Windows.Forms.DataGridView dataGridView)
        {
            List<int> listSelect = new List<int>();
            foreach (System.Windows.Forms.DataGridViewRow row in dataGridView.SelectedRows)
            {
                listSelect.Add(row.Index);
            }
            for (int i = listSelect.Count - 1; i > -1; i--)
            {
                dataGridView.Rows.RemoveAt(listSelect[i]);
            }
            //for (int i = dataGridView.SelectedRows.Count-1; i >-1 ; i--)
            //{
            //    dataGridView.Rows.RemoveAt(dataGridView.SelectedRows[i].Index);
            //}
            if (dataGridView.Rows.Count > 0)
            {
                if (dataGridView.Rows.Count > listSelect[listSelect.Count - 1] && dataGridView.Rows.Count >= 1)
                {
                    //dataGridView
                    dataGridView.Rows[listSelect[listSelect.Count - 1]].Selected = true;
                    dataGridView.CurrentCell = dataGridView.Rows[listSelect[listSelect.Count - 1]].Cells[0];
                }
                else
                {
                    dataGridView.Rows[listSelect[listSelect.Count - 1] - 1].Selected = true;
                    dataGridView.CurrentCell = dataGridView.Rows[listSelect[listSelect.Count - 1] - 1].Cells[0];
                }
            } 
        }


        public void FormAddRecord(System.Windows.Forms.DataGridView dataGridView, Dictionary<string, string> dic)
        {
            throw new NotImplementedException();
        }




        public void FormAddRecord(System.Windows.Forms.DataGridView gridView)
        {


            for (int i = 0; i < gridView.ColumnCount; i++)
            {
                //int j = 0;
                //while (gridView.Rows[i].Cells[])
                gridView[gridView.Columns[i].HeaderText, gridView.Rows.Count - 1].Value ="新建"+gridView.Columns[i].HeaderText + i;
            }
        }
    }
   
    class FormOperatorTreeList : IFormOperator<DevExpress.XtraTreeList.TreeList>
    {
        public void FormDeleteRecord(DevExpress.XtraTreeList.TreeList treeList)
        {
            List<int> listSelect = new List<int>();
            //listSelect.AddRange(treeList.)
            treeList.DeleteSelectedNodes();
        }
        public void FormAddRecord(DevExpress.XtraTreeList.TreeList treeList, Dictionary<string, string> dic)
        {
            throw new NotImplementedException();
        }




        public void FormAddRecord(DevExpress.XtraTreeList.TreeList gridView)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 表单的增加删除操作类
    /// </summary>
    public class FormOperatorService
    {
        static FormOperatorGridView formOperator;
        static FormOperatorGridView _formOperator
        {
            get
            {
                if (formOperator == null)
                {
                    formOperator = new FormOperatorGridView();
                }
                return formOperator;
            }
        }
        dic) {             _formOperator.FormAddRecord (gridView, dic);         }
















        /// <summary>
        /// Set the new column name
        /// </summary>
        /// <param name="dt">DataTable table containing data</param>
        /// <param name="fielName ">The corresponding column name in the database</param>
        /// <returns>The name of the new row</returns>
        public static string SetNewRowFielName(DataTable dt, string fielName)
        {
            int newRowNum = 1;
            bool exis = true;
            string name = string.Empty;
            DataRow[] row = null;
            try
            {
                while (exis || string.IsNullOrEmpty(name))
                {
                    name = string.Format("{0}_{1}", fielName,newRowNum);
                    row = dt.Select(string.Format("{0}='{1}'", fielName, name));
                    if (row != null && row.Length > 0)
                    {
                        newRowNum++;
                    }
                    else
                    {
                        exis = false;
                    }
                }
            }
            catch
            {
                return name;
            }
            return name;
        }

    }

The following is an example of the call

//使用方法如下:
string RecordFormID = Guid.NewGuid().ToString();
string Name="Name";
string Description = "Description";
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("RecordFormID", RecordFormID);
dic.Add(Name, FormOperatorService.SetNewRowFielName(CLDOverhaulRecordTableClone,Name));
dic.Add(Description, FormOperatorService.SetNewRowFielName(CLDOverhaulRecordTableClone,Description));
FormOperatorService.FormAddRecord(CLDOverhaulRecordView, dic);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768151&siteId=291194637