C#中DataGridView滑鼠左鍵雙擊事件的新增
阿新 • • 發佈:2019-01-09
在DataGridView中檢視資料,有時需要進行單個數據的詳細分析,那麼用到滑鼠雙擊事件就很方便了。
首先,要在DataGridView所在的Designer.cs中新增滑鼠雙擊事件;找到DataGridView在Designer.cs中的位置,新增以下程式碼
this.dataGridView1.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.DataGridViewListCellMouseDoubleClick);
這樣就完成了事件的新增(注意:上面程式碼中紅色字表示的滑鼠雙擊事件的觸發方法,名稱必須與介面程式碼中的方法名相同
第二步,在介面程式碼中編寫程式碼,定義滑鼠雙擊後的操作
//滑鼠雙擊列表操作 private void DataGridViewListCellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { //判斷雙擊的是否為標題 if (e.RowIndex >= 0) { DataTable table =(DataTable) dataGridView1.DataSource;//資料來源 string id = table.Rows[e.RowIndex]["bikeId"].ToString(); //測試 //MessageBox.Show("id:"+id); //當前日期 DateTime data = System.DateTime.Today; //根據引數獲取歷史資料 data_History = Data.GetDataInfoFromUrl(id, "20150101", data.ToString("yyyyMMdd")); //展示窗體物件(新增資料,然後展示) BikeAllHistoryInfomationForm bikeHistoryData = new BikeAllHistoryInfomationForm(); bikeHistoryData.BikeHistoryData(bikeId,data_History); bikeHistoryData.Show(); } }
這樣就完成了DataGridView滑鼠雙擊事件的新增