站內文章

2014年10月27日 星期一

在datagridview中產生按鈕進行點選


一開始先在pageload建立按鈕

                // 建立 PDA下載 按鈕
                // new 一個datagridview的欄位 名稱為DownPDA
                DataGridViewButtonColumn DownPDA = new DataGridViewButtonColumn(); 
                DownPDA.HeaderText = "PDA下載"; // 按鈕名稱
                DownPDA.CellTemplate.Style.BackColor = Color.Pink; // 設定按鈕顏色
                dataGridView1.Columns.Insert(8,DownPDA); // 在第8欄插入 剛建好的按鈕欄位
                (dataGridView1.Columns[8] as DataGridViewButtonColumn).DefaultCellStyle.NullValue = "PDA下載";

                // gridview自動充滿顯示區
                // 調整可參考 http://www.cnblogs.com/scottckt/archive/2007/10/09/917874.html
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

建一個datagridview的 CellClick (接受點擊事件)


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 如果點了第8欄則進行
            if (e.ColumnIndex == 8)
            {
                // 抓取點選那一行的 第1欄位的資料 (補充:欄位是從0起始開始數)
                string DeviceLotNM = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                // 抓取點選那一行的 第2欄位的資料
                string DeviceCount = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            }
        }

沒有留言:

張貼留言