2017-07-07 67 views
0

我正在使用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()); 
     } 
    } 

第一次的海报,很长一段时间的潜伏者。

+0

什么,你如何填充的DataGridView?每一行都是一个类的实例吗? –

+0

DataGridView的DataSource使用以下内容填充: dbcontext.Authors .OrderBy(author => author.LastName) 。ThenBy(author => author.FirstName) .Load(); authorBindingSource.DataSource = dbcontext.Authors.Local; – JFarkas

+1

所以我认为'Author'是某个地方的类。如果在该事件方法中插入'var currentAuthor =(Author)authorDataGridView.CurrentRow;',程序是否会编译?如果是这样,'currentAuthor'的价值是什么? –

回答

0

您可以获得对该行的引用,从而简化了一下。

DataGridViewRow authorRow = authorDataGridView.Rows[e.RowIndex]; 
if (authorRow.Cells[e.ColumnIndex].Value != null) 
{ 
    MessageBox.Show("Author Number: " + authorRow.Cells[0].Value.ToString() 
      + "\nAuthor First Name: " + authorRow.Cells[1].Value.ToString() 
      + "\nAuthor Last Name: " + authorRow.Cells[2].Value.ToString()); 
} 

如果你想在列标题文本,我认为它:

DataGridViewColumn col = authorDataGridView.Column[e.ColumnIndex] 

,然后你可以从标题文字:如果您打算做

col.HeaderText; 

像这样的东西很多,你可以封装你想要的功能:

public string ParseRowText(DataGridView grid, DataGridViewCellEventArgs e) 
    { 
     DataGridViewRow row = grid.Rows[e.RowIndex]; 
     string output = ""; 
     for (int col = 0; col < grid.Columns.Count; col++) 
     { 
      //Get column header and cell contents 
      output = output + grid.Columns[col].HeaderText + ": " + row.Cells[col].Value.ToString() + Environment.NewLine; 
     } 
     return output; 
    } 

这是你之后的事情,列头是“作者号”,“作者名”,“作者姓”等?

你会然后通过调用它:

private void authorDataGridView_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    MessageBox.Show(ParseRowText(this, e)); 
} 
0

你可以在模型类添加你在DGV的显示对象的显示方法:

public class Author 
{ 
    public int Number { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public void Show() 
    { 
    MessageBox.Show($"Author Number: {Number}\nAuthor First Name: {FirstName}\nAuthor Last Name: {LastName}"); 
    } 

这样你可以轻松地在您的CellClick中调用它 - 方法

private void authorDataGridView_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    var author = ((DataGridView)sender).Rows[e.RowIndex].DataBoundObject 
    //now you have an author-type object and just call your show method 
    author.Show() 
} 

只要您通过使用DataSourc将项目放入Dgv e道具,您可以随时使用DataBoundObject属性重新获取它们。

0

鉴于您正在使用BindingSource,您还可以使用BindingSource.Current属性来实现此目的 - 事实上,我更喜欢这样做,因为您可以从任何其他事件/方法访问DataGridView中当前选定的行(例如一个按钮点击)。

private void btnShow_Click(object sender, EventArgs e) 
{ 
    var currentAuthor = (Author)authorBindingSource.Current; 
    MessageBox.Show(currentAuthor.Name); 
} 

也表明作者在期望的格式属性,你可以重写ToString()方法,然后就做一个MessageBox.Show(currentAuthor.ToString())。

public class Author 
{ 
    public int Number { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public override string ToString() 
    { 
     return $"Author Number: {Number}\nAuthor First Name: {FirstName}\nAuthor Last Name: {LastName}"; 
    } 
} 

请参阅本MSDN帮助更多信息BindingSource.Current