2017-07-07 68 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