private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e) {
//及时更新点击行数据
this.gridView1.UpdateCurrentRow(); GridView view = (GridView)sender; //点击行 DataRow dr = view.GetFocusedDataRow(); //设置列属性 gridView1.Columns[列名].OptionsColumn.AllowEdit = false; gridView1.Columns[列名].OptionsColumn.ReadOnly = true; }
//http://www.cnblogs.com/wuhuacong/archive/2012/07/17/2595182.html
//将gridView1转换为datatable
m_tblDataList = ((DataView)this.gridView1.DataSource).Table.Copy();
//添加表格列
m_tblDataList.Columns.Add(t_stockindetail.gwet, typeof(float));
//设置表格列默认值
m_tblDataList.Columns[t_stockindetail.gwet].DefaultValue = 0;
//宽度自适应,可以出现滚动条
this.gridView1.BestFitColumns();
//宽度自适应,但是不出现滚动条
gridView1.OptionsView.ColumnAutoWidth = true;
namespace RowCellStyle { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("a"); dt.Columns.Add("s"); dt.Columns.Add("d"); dt.Columns.Add("f"); DataRow dr = dt.NewRow(); dt.Rows.Add(dr); gridControl1.DataSource = dt; } private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { try { DataTable dt = (DataTable)gridControl1.DataSource; if (!string.IsNullOrEmpty(dt.Rows[(dt.Rows.Count - 1)]["a"].ToString())) { DataRow dr = dt.NewRow(); dt.Rows.Add(dr); gridControl1.DataSource = dt; } bool isCheck = true; for (int i = 0; i < dt.Rows.Count -2; i++) { if (dt.Rows[i]["a"].ToString().Equals(dt.Rows[(dt.Rows.Count - 2)]["a"].ToString())) { MessageBox.Show("主键不能相同"); isCheck = false; break; } } if (!isCheck) { return; } GridView view = (GridView)sender; //获取选中行的列a的值 string str_a = view.GetFocusedRowCellValue("a").ToString(); SetGridStyle(str_a); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { try { GridView view = (GridView)sender; //获取选中行的列a的值 string str_a = view.GetFocusedRowCellValue("a").ToString(); SetGridStyle(str_a); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } /// <summary> /// 点击行的主key(主key为空不能编辑点击行其他内容,反之可以) /// </summary> /// <param name="PrimarValue">主Key值</param> private void SetGridStyle(string PrimarValue) { try { DataTable dt = (DataTable)gridControl1.DataSource; gridView1.OptionsBehavior.Editable = true; //如果主Key为空,设置除了主Key其他不可编辑 //如果主Key不为空,设置都可编辑 if (string.IsNullOrEmpty(PrimarValue)) { foreach (DataColumn item in dt.Columns) { gridView1.Columns[item.ColumnName.ToString()].OptionsColumn.AllowEdit = false; gridView1.Columns[item.ColumnName.ToString()].OptionsColumn.ReadOnly = true; gridView1.Columns[item.ColumnName.ToString()].OptionsColumn.AllowFocus = false; } gridView1.Columns["a"].OptionsColumn.AllowFocus = true; gridView1.Columns["a"].OptionsColumn.AllowEdit = true; gridView1.Columns["a"].OptionsColumn.ReadOnly = false; } else { foreach (DataColumn item in dt.Columns) { gridView1.Columns[item.ColumnName.ToString()].OptionsColumn.AllowEdit = true; gridView1.Columns[item.ColumnName.ToString()].OptionsColumn.ReadOnly = false; gridView1.Columns[item.ColumnName.ToString()].OptionsColumn.AllowFocus = true; } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } /// <summary> /// 事件相当于一直在遍历所有cells /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) { DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView; //列名=a,值=1的标记为绿色 if (e.CellValue.ToString() == "1" && e.Column.FieldName.ToString().Equals("a")) { e.Appearance.BackColor = Color.FromArgb(128, 255, 128); e.Column.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"; e.Column.Width = 250; } } } }