次の方法で共有


DataGridViewCellValueEventArgs.ColumnIndex プロパティ

定義

イベントが発生したセルの列インデックスを示す値を取得します。

public:
 property int ColumnIndex { int get(); };
public int ColumnIndex { get; }
member this.ColumnIndex : int
Public ReadOnly Property ColumnIndex As Integer

プロパティ値

イベントが発生したセルを含む列のインデックス。

次のコード例では、 プロパティを使用 ColumnIndex してデータ ストアからセル値を取得する方法を示します。 この例は、「方法: Windows フォーム DataGridView コントロールで仮想モードを実装する」で提供されるより大きな例の一部です。

void dataGridView1_CellValueNeeded( Object^ /*sender*/,
    System::Windows::Forms::DataGridViewCellValueEventArgs^ e )
{
   Customer^ customerTmp = nullptr;
   
   // Store a reference to the Customer object for the row being painted.
   if ( e->RowIndex == rowInEdit )
   {
      customerTmp = this->customerInEdit;
   }
   else
   {
      customerTmp = dynamic_cast<Customer^>(this->customers[ e->RowIndex ]);
   }
   
   // Set the cell value to paint using the Customer object retrieved.
   int switchcase = 0;
   if ( (this->dataGridView1->Columns[ e->ColumnIndex ]->Name)->Equals( L"Company Name" ) )
         switchcase = 1;
   else
   if ( (this->dataGridView1->Columns[ e->ColumnIndex ]->Name)->Equals( L"Contact Name" ) )
         switchcase = 2;


   switch ( switchcase )
   {
      case 1:
         e->Value = customerTmp->CompanyName;
         break;

      case 2:
         e->Value = customerTmp->ContactName;
         break;
   }
}
private void dataGridView1_CellValueNeeded(object sender,
    System.Windows.Forms.DataGridViewCellValueEventArgs e)
{
    // If this is the row for new records, no values are needed.
    if (e.RowIndex == this.dataGridView1.RowCount - 1) return;

    Customer customerTmp = null;

    // Store a reference to the Customer object for the row being painted.
    if (e.RowIndex == rowInEdit)
    {
        customerTmp = this.customerInEdit;
    }
    else 
    {
        customerTmp = (Customer)this.customers[e.RowIndex];
    }

    // Set the cell value to paint using the Customer object retrieved.
    switch (this.dataGridView1.Columns[e.ColumnIndex].Name)
    {
        case "Company Name":
            e.Value = customerTmp.CompanyName;
            break;

        case "Contact Name":
            e.Value = customerTmp.ContactName;
            break;
    }
}
Private Sub dataGridView1_CellValueNeeded(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) _
    Handles dataGridView1.CellValueNeeded

    ' If this is the row for new records, no values are needed.
    If e.RowIndex = Me.dataGridView1.RowCount - 1 Then
        Return
    End If

    Dim customerTmp As Customer = Nothing

    ' Store a reference to the Customer object for the row being painted.
    If e.RowIndex = rowInEdit Then
        customerTmp = Me.customerInEdit
    Else
        customerTmp = CType(Me.customers(e.RowIndex), Customer)
    End If

    ' Set the cell value to paint using the Customer object retrieved.
    Select Case Me.dataGridView1.Columns(e.ColumnIndex).Name
        Case "Company Name"
            e.Value = customerTmp.CompanyName

        Case "Contact Name"
            e.Value = customerTmp.ContactName
    End Select

End Sub

注釈

このオフセットを Columns プロパティと共に使用して、イベントが発生するセルを参照します。

適用対象

こちらもご覧ください