我正在使用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'的价值是什么? –