欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dev GridContol控件常用到的代码

程序员文章站 2022-07-13 21:30:18
...

Dev GridContol控件常用到的

  int focusedRowHandle = this.gridView1.FocusedRowHandle;  //获取表格中操作的哪一行
  strPojectNo = this.gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "列名"); //获取点击列的列名
 this.gridView1.OptionsBehavior.Editable = false;//禁止表格所有单元格输入
 this.gridView2.Columns["列名"].OptionsColumn.AllowEdit = false;  //指定那一列禁止输入
 this.gridView1.RowCount <= 0; //判断表格是否有数据
 gridView1.Columns["列名"].Visible = false;//对列表进行隐藏
 bool flag =dataRow.RowState == DataRowState.Modified; //判断表格行内容有无更改状态
 
 this.gridControl1.DataSource = dt;  //对查询到的数据表DataTable,绑定数据到表格
 this.gridView1.BestFitColumns();//对列名之间间隔根据单元格内聚长度自动调整间距(注意:做查询数据量大时会很慢)

#改变某一单元格的值,另外一单元格也会跟着变动,输入指定的值
        /// <summary>
        /// 改变收货数量,入库数量,出货数量单元格值收货时间,入库时间,出货时间赋予当前时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gridView2.RowCount > 0 && e.RowHandle >= 0)
            {
                string fileName = gridView2.FocusedColumn.FieldName; //获取正在操作列的列名
                int i = e.RowHandle; //获取正在操作的行数,千万别用for循环来遍历gridview的行数
                switch (fileName)
                {
                    case "收货数量":
                      //判断收货时间单元格是否为空或者收货数量单元格的值有没有变化,如果为空或者有变化就更新收货时间单元格的时间,反之,不更新
                        if (gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "收货时间").ToString() ==""|| gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "收货数量").ToString() != text)
                        {
                            this.gridView2.GetDataRow(i)["收货时间"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        }
                       
                        break;
                    case "入库数量":
                        if (gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "入库时间").ToString() == "" || gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "入库数量").ToString() != text1)
                        {
                            this.gridView2.GetDataRow(i)["入库时间"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        }
                 
                        break;
                    case "出库数量":
                        if (gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "出库时间").ToString() == "" || gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "出库数量").ToString() != text2)
                        {
                            this.gridView2.GetDataRow(i)["出库时间"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        }//此处判断很重要
                        break;
                }
            }
        }


        /// <summary>
        /// 在更改值的时候获取当前单元格的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView2_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
          
                if (gridView2.RowCount > 0)
                {
                   text = gridView2.GetRowCellDisplayText(this.gridView2.FocusedRowHandle, "收货数量").ToString();
                   text1 = gridView2.GetRowCellDisplayText(this.gridView2.FocusedRowHandle, "入库数量").ToString();
                   text2 = gridView2.GetRowCellDisplayText(this.gridView2.FocusedRowHandle, "出库数量").ToString();
                }
        }