C#——dataGridView控件獲取當前鼠標所在的行


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //獲取行列坐標索引
            //方法一:
            //int row = e.RowIndex+1;
            //int col = e.ColumnIndex+1;
 
            //方法二:
            //int row = dataGridView1.CurrentCell.RowIndex + 1;
            //int col = dataGridView1.CurrentCell.ColumnIndex + 1;
 
            //方法三:
            //int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col=dataGridView1.CurrentCellAddress.X+1;
 
            //方法四:
            int row = dataGridView1.CurrentRow.Index + 1;
             
            //獲取當前單元格內容
            //方法1:
 
           // string cell = dataGridView1.Rows[row-1].Cells[col-1].Value.ToString();
 
            //方式2:
            string cell = dataGridView1.CurrentCell.Value.ToString();
 
            MessageBox.Show("點擊:"+row+"行;"+col+"列\n內容是:"+cell);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM