dev gridcontrol 單選框效果

衣舞晨風發表於2015-03-12
  private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            //此處4是單選框所在的列號
            if (e.Column.ColumnHandle != 4)
            { 
                return;
            }
            for (int i = 0; i < gridView1.DataRowCount; i++)
            {
                //非當前行 置為 未選中狀態
                if (i != e.RowHandle)
                { 
                    //0表示 未選中
                    gridView1.GetDataRow(i)[e.Column.ColumnHandle] = "0";
                }
            }
        }


此時gridView1 .OptionsSelection.MultiSelect 設定與否 都不影響結果。



方法二:

  #region gridView1_RowCellClick事件
        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left) // 判斷是否是用滑鼠點選  
            {
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo ghi = gridView1.CalcHitInfo(new Point(e.X, e.Y));
                if (ghi.InRow)  // 判斷游標是否在行內  
                {
                    //清空勾選項
                    for (int i = 0; i < gridView1.DataRowCount; i++)
                    {
                        if (gridView1.FocusedRowHandle != i)
                        {
                            gridView1.GetDataRow(i)["selected"] = false;
                        }
                    }
                    if ((bool)gridView1.GetDataRow(ghi.RowHandle)["selected"] == true)
                    {
                        gridView1.GetDataRow(e.RowHandle)["selected"] = false;
                    }
                    else
                    {
                        gridView1.GetDataRow(e.RowHandle)["selected"] = true;
                    }
                }
            }
        }
        #endregion
  #region  單選框控制
        private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.ColumnHandle != 0)
            {
                return;
            }
            for (int i = 0; i < gridView1.DataRowCount; i++)
            {
                //非當前行 置為 未選中狀態  
                if (i != e.RowHandle)
                {
                    //0表示 未選中  
                    gridView1.GetDataRow(i)[e.Column.ColumnHandle] = "0";
                }
            }
        }
        #endregion


作者:jiankunking 出處:http://blog.csdn.net/jiankunking

相關文章