1. 程式人生 > 實用技巧 >DevExPress GridView獲取單元格座標和內容

DevExPress GridView獲取單元格座標和內容

 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 + "\n" + msg);
17         }