Devexpress GridView部分常用操作总结

 一:Clone返回新的 DataTable

Clone返回新的 DataTable,与当前的 DataTable 具有相同的架构;Copy:返回新的 DataTable,它具有与该 DataTable 相同的结构(表架构和约束)和数据 

DataRow[] _dr = DT.Select("CLSSBH=‘"+ Filter_Str +"‘"); 
DataTable dt = DT.Clone() ; 
DataRow dr ; 
for( int j = 0 ;j < _dr.Length ;j ++ ) 

dr = dt.NewRow() ; 
dr = _dr[j] ; 
dt.ImportRow( dr ) ; 

  
我一般使用DataTable.Rows.Add(row.ItemArray); 

 二:选中某一行

GridView.FocusedRowHandle =i;
GridView.SelectRow(i);

三:遍历GridView

for (int i = 0; i < gridView1.RowCount; i++)
{
       for (int j = 0; j < gridView1.Columns.Count; j++)
       {
             object val = gridView1.GetRowCellValue(i, gridView1.Columns[j]);
       }
}

四:单元格双击响应

需要先将gridview1.OptionsBehavior.Editable设为false,然后响应gridControl1_DoubleClick事件。

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            MouseEventArgs arg = e as MouseEventArgs;
            if (arg == null)
                return;

            GridHitInfo hitInfo = gridView1.CalcHitInfo(new Point(arg.X, arg.Y));//获取坐标点
            if (hitInfo.RowHandle >= 0)
            {
                DataRow row = gridView1.GetDataRow(hitInfo.RowHandle);
                _list.Clear();
                _list.Add(row[0].ToString());
                gisResoureMonControl1.SetSelectResource(_list);
            }           
        }

五:GridView隐藏行

需要响应CustomRowFilter事件,用来设置过滤条件;

需要根据原始数据源结合起来,才能方便地隐藏或者显示行;

        /// <summary>
        /// 根据条件查询并在表格中显示结果
        /// </summary>
        /// <param name="strCondition"></param>
        private void QueryTable(string strCondition)
        {
            DataTable table = gridControl1.DataSource as DataTable;
            if (table == null)
                return;

            _indexlist.Clear();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    if(table.Rows[i][j].ToString().Contains(strCondition))
                    {
                        _indexlist.Add(i);
                        gridView1.RefreshData();
                        break;
                    }
                }
            }

            if (_indexlist.Count==0)
                HideGridView();                      
        }

        /// <summary>
        /// gridView的行隐藏或显示的过滤条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_CustomRowFilter(object sender, RowFilterEventArgs e)
        {
            if (_indexlist.Count == 0)
                return;

            if(_indexlist.Contains(-2))
                e.Visible = true;   //全显示
            else if (_indexlist.Contains(-3))
                e.Visible = false;   //全隐藏
            else if(_indexlist.Contains(e.ListSourceRow))
                e.Visible = true;
            else
                e.Visible = false;

            e.Handled = true;
        }

Devexpress GridView部分常用操作总结

上一篇:系统学习docker


下一篇:ARM探索之旅 | 二、ARM Cortex-M 用什么指令集?