我正在使用CellClick打開一個消息框,並從窗體中的DataGridView中顯示單行的行內容。我被困在試圖找到比迄今爲止更優雅的東西。我正在尋找一種方法來顯示列標題的內容和行值,可能使用foreach或for循環,但無濟於事。任何建議?DataGridView檢索整行
private void authorDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (authorDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show("Author Number: " + authorDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()
+ "\nAuthor First Name: " + authorDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString()
+ "\nAuthor Last Name: " + authorDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString());
}
}
第一次的海報,很長一段時間的潛伏者。
什麼,你如何填充的DataGridView?每一行都是一個類的實例嗎? –
DataGridView的DataSource使用以下內容填充: dbcontext.Authors .OrderBy(author => author.LastName) 。ThenBy(author => author.FirstName) .Load(); authorBindingSource.DataSource = dbcontext.Authors.Local; – JFarkas
所以我認爲'Author'是某個地方的類。如果在該事件方法中插入'var currentAuthor =(Author)authorDataGridView.CurrentRow;',程序是否會編譯?如果是這樣,'currentAuthor'的價值是什麼? –