2017-02-13 15 views
1

によって引き起こされる細胞の変化のためのイベントは、DataSourceの変化の結果として、DataGridViewの中ときのセル値の変更のためのイベントはありますか?のC#のDataGridView - データソース

私は現在のセルの値が自動的に更新、

public class CustomWorkbook : INotifyPropertyChanged 
{ 
    string filepath; 
    string status; 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    ... 
} 

をINotifyPropertyChangedのを実装して独自のカスタムクラスを作成し、次のように私のDataGridViewにそれをバインドされ、

BindingList<CustomWorkbook> workbookList = new BindingList<CustomWorkbook>(); 
BindingSource workbookBinding = new BindingSource(workbookList , null); 
dataGridViewWorkbooks.DataSource = workbookBinding; 

DataGridView

ました必要に応じて、セルの値にcがあることを知る必要がある、追加したいと思う処理や美的効果がいくつかありますどのセル(すなわち、 UPDATED細胞緑、PENDING細胞黄)

を作る私はDataGridViewの中CellValueChangedイベントを試してみたが、それはユーザーのみが編集のために働くようです。 NotifyPropertyChangedイベントは、値が変更されたときに発生しますが、変更されたセルへの参照は一切与えられません。

答えて

1

ほとんどのDataGridViewイベントにイベントハンドラを追加するだけの強引なアプローチをとった後、私はDataBindingCompleteイベントが私の後だったことに気付きました。このイベントは、CustomWorkbookクラスのプロパティが変更されたときに発生しました(INotifyPropertyChangedBindingListBindingSource、最後にDataSourceDataGridViewになります)。

このイベントが変更されたプロパティに対応するセルへの参照を提供していませんが、私は問題のセルを含む列の名前を知っているので、ちょうどすべてのセルをループ終わりました。

/// <summary> 
    /// Called whenever changes have been made to the binded list 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void DataGridViews_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
    { 
     DataGridViews_UpdateStatusColour(sender as DataGridView); 
    } 

    /// <summary> 
    /// Change the colour of the cells in the column whose DataPropertyName is "Status" 
    /// </summary> 
    /// <param name="grid"></param> 
    private void DataGridViews_UpdateStatusColour(DataGridView grid) 
    { 
     // Get the column index 
     int targetColumn = 0; 
     foreach (DataGridViewColumn col in grid.Columns) 
     { 
      if (col.DataPropertyName == "Status") 
      { 
       targetColumn = col.Index; 
       break; 
      } 
     } 

     // Loop through every row, and colour the corresponding cell 
     foreach (DataGridViewRow row in grid.Rows) 
     { 
      DataGridViewCell cell = row.Cells[targetColumn]; 
      switch (cell.Value.toString()) 
      { 
       case ("UPDATED"): 
        cell.Style.BackColor = System.Drawing.Color.Green; 
        cell.Style.SelectionBackColor = System.Drawing.Color.Green; 
        break; 
       case ("PENDING"): 
        cell.Style.BackColor = System.Drawing.Color.Orange; 
        cell.Style.SelectionBackColor = System.Drawing.Color.Orange; 
        break; 
       case ("MISSING"): 
        cell.Style.BackColor = System.Drawing.Color.LightSalmon; 
        cell.Style.SelectionBackColor = System.Drawing.Color.LightSalmon; 
        break; 
       case ("ERROR"): 
        cell.Style.BackColor = System.Drawing.Color.Red; 
        cell.Style.SelectionBackColor = System.Drawing.Color.Red; 
        break; 
       default: 
        break; 
      } 
     } 
    } 

それがどのように見える:

DataGridView with colour

+0

あなたはDataGridViewのプロパティselectedCellsにとCurrentCellを見てみたいことがあり - あなたはどのセル(複数可)編集された絞込みに役立つかもしれないこと。 – lyrisey

関連する問題