VB.net / C# 如何用程式觸發DataGridView的CellClick事件? [ GOOGLE C#datagridview Click ][任何事件函數程式碼驅動呼叫]
VB.net / C# 如何用程式觸發DataGridView的CellClick事件? [ GOOGLE C#datagridview Click ][任何事件函數程式碼驅動呼叫/程式模擬產生對應事件]
資料來源: https://www.blueshop.com.tw/board/FUM20050124191756KKC/BRD20101005194219HI5.html
VB.net Code:
Public Class Form1 Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick Debug.Print(sender.value) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DataGridView1_CellClick(DataGridView1.Rows(0).Cells(0), New System.Windows.Forms.DataGridViewCellEventArgs(0, 0)) DataGridView1_CellClick(DataGridView1.Rows(1).Cells(0), New System.Windows.Forms.DataGridViewCellEventArgs(0, 1)) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load '測試用 DataGridView1.Columns.Add("a1", "a1") DataGridView1.Rows.Add() DataGridView1.Rows.Add() DataGridView1.Rows(0).Cells(0).Value = "test1" DataGridView1.Rows(1).Cells(0).Value = "test2" End Sub End Class
C# code:
if(dgv001.Rows.Count>0) { //DataGridView1_CellClick(DataGridView1.Rows(0).Cells(0), New System.Windows.Forms.DataGridViewCellEventArgs(0, 0)) dgv001_Click(dgv001.Rows[0].Cells[1], new EventArgs()); } //--------------------------------------------- private void dgv001_Click(object sender, EventArgs e)//左側列表 選擇事件觸發 { String SQL = ""; if (dgv001.SelectedRows.Count > 0) { m_cust_display_dataNew = null; m_cust_display_dataNew = new cust_display_data(); int index = dgv001.SelectedRows[0].Index;//取得被選取的第一列旗標位置 int intSID = Convert.ToInt32(dgv001.Rows[index].Cells[0].Value.ToString()); SQL = String.Format("SELECT * FROM cust_display_data WHERE SID='{0}' LIMIT 0,1", intSID); DataTable dt = SQLDataTableModel.GetDataTable(SQL); if ((dt != null) && (dt.Rows.Count > 0)) { m_cust_display_dataNew.m_SID = Convert.ToInt32(dt.Rows[0]["SID"].ToString());// " int NOT NULL, m_cust_display_dataNew.m_data_name = dt.Rows[0]["data_name"].ToString();// varchar(50), m_cust_display_dataNew.m_data_kind = dt.Rows[0]["data_kind"].ToString();//" char(2), m_cust_display_dataNew.m_source_type = dt.Rows[0]["source_type"].ToString();// V: Video I : Image T:文字資料 W:WebURL[UI沒有URL選項] m_cust_display_dataNew.m_stretch_size = dt.Rows[0]["stretch_size"].ToString();// N / Y 是否自動延展 Y:是 m_cust_display_dataNew.m_play_type = dt.Rows[0]["play_type"].ToString();//A: Auto S : 依據設定變動的秒數 m_cust_display_dataNew.m_play_speed_sec = Convert.ToInt32(dt.Rows[0]["play_speed_sec"].ToString());//sec m_cust_display_dataNew.m_del_flag = dt.Rows[0]["del_flag"].ToString();// N / Y m_cust_display_dataNew.m_created_time = Convert.ToDateTime(dt.Rows[0]["created_time"].ToString()).ToString("yyyy-MM-dd HH:mm:ss.fff");// timestamp, m_cust_display_dataNew.m_updated_time = Convert.ToDateTime(dt.Rows[0]["updated_time"].ToString()).ToString("yyyy-MM-dd HH:mm:ss.fff");// timestamp, dt = null; SQL = String.Format("SELECT * FROM cust_display_content WHERE display_data_sid='{0}'", intSID); dt = SQLDataTableModel.GetDataTable(SQL); if ((dt != null) && (dt.Rows.Count > 0)) { for (int i = 0; i < dt.Rows.Count; i++) { cust_display_content cust_display_contentBuf = new cust_display_content(); cust_display_contentBuf.m_item_no = Convert.ToInt32(dt.Rows[i]["item_no"].ToString()); cust_display_contentBuf.m_content = dt.Rows[i]["content"].ToString(); m_cust_display_dataNew.m_display_contents.Add(cust_display_contentBuf); } } } DB2tabP003RightUI(); } }