1 public Form1() 2 { 3 InitializeComponent(); 4 gridControl1.DataSource = CreatDataTable(); 5 gridView1.OptionsBehavior.Editable = false; 6 //gridView1.Columns["Name"].OptionsColumn.AllowEdit = false;//设置列不可以编辑 7 8 //gridView1.Columns["ID"].OptionsColumn.AllowEdit = false;//设置列不可以编辑 9 } 10 11 12 private DataTable CreatDataTable() 13 { 14 DataTable dt = new DataTable();//创建表 15 DataColumn dc = new DataColumn(); 16 dc.Caption = "编号"; 17 dc.ColumnName = "ID"; 18 dc.DataType = typeof(int); 19 dt.Columns.Add(dc);//添加列 20 dt.Columns.Add("Name", typeof(String)); 21 dt.Columns.Add("Age", typeof(Int32)); 22 dt.Columns.Add("Data", typeof(decimal)); 23 dt.Columns.Add("date of birth", typeof(DateTime)); 24 dt.Rows.Add(new object[] { 1, "张三", 20, -8.1233, "2011-1-1 17:52:23" });//添加行 25 dt.Rows.Add(new object[] { 1, "张三", 21, 9.8769, "2011-2-1 17:42:23" });//添加行 26 dt.Rows.Add(new object[] { 2, "李四", 25, 7.2367, "2011-1-5 17:52:53" }); 27 dt.Rows.Add(new object[] { 3, "王五", 30, -5.8963, "2011-5-1 16:24:23" }); 28 return dt; 29 }
1 private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) 2 { 3 //获取点击单元格左上角的坐标 4 GridViewInfo info = gridView1.GetViewInfo() as GridViewInfo; 5 GridCellInfo cellInfo = info.GetGridCellInfo(gridView1.FocusedRowHandle, gridView1.FocusedColumn); 6 string m = cellInfo.Bounds.X.ToString(); 7 string m2 = cellInfo.Bounds.Y.ToString(); 8 string msg = cellInfo.GetCellValue().ToString(); //获取单元格数据 9 10 11 //获取鼠标点击时的坐标 12 Point p = this.gridControl1.PointToClient(Control.MousePosition); 13 string n = p.X.ToString(); 14 string n2 = p.Y.ToString(); 15 16 MessageBox.Show("单元格坐标:" + m + "," + m2 + " " + msg); 17 }