首先,c1Flexgrid這個控件不像vs里自帶的DataGridView那樣有CellClick事件可以直接獲取用戶點擊的單元格信息。
注意:在c1Flexgrid里只能通過 click事件 處理 所以只能通過 HitTestInfo對象 把鼠標點下的坐標信息 變成我們需要的單元格信息(這個方法的到的左邊可以避免 在點擊空白以外的地方還是獲取在這這之前獲得焦點的單元格坐標信息)。
1 首先在click事件里 吧此事件的數據基類轉換為 我們需要的 MouseEventArgs類型。
2 把MouseEventArgs.Location得到的坐標信息作為參數提供 給c1Flexgrid 控件的HitTest()方法 ,這樣就可以得到一個HitTestInfo對象。
3 根據需要 判斷 HitTestInfo對象的坐標點 不是行頭,列頭並且不是 數據單元格以為的地方。(也可以加上mouseEvent.Button == MouseButtons.Left 判斷是不是鼠標左鍵的點擊)
4 根據得到的橫坐標 或者縱坐標 使用c1FlexGrid1.Rows[][] 二維數組進行定位單元格,想得到什么還不是輕而易舉。
下面是項目中實例代碼
1 // 通過HitTestInfo 根據坐標 取得所在行序號的相關值 2 private void c1FlexGrid1_Click(object sender, EventArgs e) 3 { 4 MouseEventArgs mouseEvent = e as MouseEventArgs; 5 if (mouseEvent.Button == MouseButtons.Left) 6 { 7 if (c1FlexGrid1.Rows != null && c1FlexGrid1.Rows[c1FlexGrid1.RowSel]["noid"] != null && c1FlexGrid1.RowSel > 0) 8 { 9 HitTestInfo hti = c1FlexGrid1.HitTest(mouseEvent.Location); 10 int currentRowIndex = hti.Row; // 也可以通過 c1FlexGrid1.RowSel,但是這個當你在點擊空白處的時候,還是取得之前的停留的焦點行列信息 11 int currentColIndex = hti.Column; 12 //點擊的地方不是行頭,列頭也不是數據以外的地方 13 if (currentRowIndex > 0 && currentColIndex > 0 && currentRowIndex < (c1FlexGrid1.Rows.Count)) 14 { 15 //獲得排序字段 16 int rankValue = int.Parse(c1FlexGrid1.Rows[currentRowIndex]["rank"].ToString().Trim()); 17 //當前的id 18 noidValue = int.Parse(c1FlexGrid1.Rows[currentRowIndex]["noid"].ToString().Trim()); 19 //當前第一條記錄id 20 firstId = int.Parse(c1FlexGrid1.Rows[1]["noid"].ToString().Trim()); 21 //當前最后條記錄id 22 lastId = int.Parse(c1FlexGrid1.Rows[c1FlexGrid1.Rows.Count-1]["noid"].ToString().Trim()); 23 //給當前的上一條記錄值 24 if (currentRowIndex>1) 25 { 26 upId = int.Parse(c1FlexGrid1.Rows[currentRowIndex - 1]["noid"].ToString().Trim()); 27 } 28 //給當前的下一條記錄值 29 if (currentRowIndex < c1FlexGrid1.Rows.Count-1) 30 { 31 donwId = int.Parse(c1FlexGrid1.Rows[currentRowIndex+1]["noid"].ToString().Trim()); 32 } 33 34 //根據情況按鈕不可用 35 butUp.Enabled = rankValue <= 1 ? false : true; 36 butTop.Enabled = rankValue <= 1 ? false : true; 37 butDonw.Enabled = rankValue >= dt.Rows.Count?false:true; 38 butBootom.Enabled = rankValue >= dt.Rows.Count ? false : true; 39 } 40 } 41 } 42 }
總之必須要根據點下的鼠標坐標來獲取選擇的單元格,不知道是不是我用的版本太低了 新版本有更新 CellClick類似的事件么