DataGridView のセル内ボタンのイベント

  VB.NET



MSDN2 内にあったサンプルです

  
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles DataGridView1.CellContentClick

    If IsANonHeaderLinkCell(e) Then
        MoveToLinked(e)
    ElseIf IsANonHeaderButtonCell(e) Then
        PopulateSales(e)
    End If
End Sub

Private Sub MoveToLinked(ByVal e As DataGridViewCellEventArgs)
    Dim employeeId As String
    Dim value As Object = DataGridView1.Rows(e.RowIndex). _
        Cells(e.ColumnIndex).Value
    If value.GetType Is GetType(DBNull) Then Return

    employeeId = CType(value, String)
    Dim boss As DataGridViewCell = _
        RetrieveSuperiorsLastNameCell(employeeId)
    If Not boss Is Nothing Then
        DataGridView1.CurrentCell = boss
    End If
End Sub

Private Function IsANonHeaderLinkCell(ByVal cellEvent As _
    DataGridViewCellEventArgs) As Boolean

    If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _
        Is DataGridViewLinkColumn _
        AndAlso Not cellEvent.RowIndex = -1 Then _
        Return True Else Return False

End Function

Private Function IsANonHeaderButtonCell(ByVal cellEvent As _
    DataGridViewCellEventArgs) As Boolean

    If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _
        Is DataGridViewButtonColumn _
        AndAlso Not cellEvent.RowIndex = -1 Then _
        Return True Else Return (False)

End Function

Private Function RetrieveSuperiorsLastNameCell( _
    ByVal employeeId As String) As DataGridViewCell

    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.IsNewRow Then Return Nothing
        If row.Cells(ColumnName.EmployeeId.ToString()). _
            Value.ToString().Equals(employeeId) Then
            Return row.Cells(ColumnName.LastName.ToString())
        End If
    Next
    Return Nothing
End Function
  



  C#



  
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

    if (IsANonHeaderLinkCell(e))
    {
        MoveToLinked(e);
    }
    else if (IsANonHeaderButtonCell(e))
    {
        PopulateSales(e);
    }
}

private void MoveToLinked(DataGridViewCellEventArgs e)
{
    string employeeId;
    object value = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
    if (value is DBNull) { return; }

    employeeId = value.ToString();
    DataGridViewCell boss = RetrieveSuperiorsLastNameCell(employeeId);
    if (boss != null)
    {
        DataGridView1.CurrentCell = boss;
    }
}

private bool IsANonHeaderLinkCell(DataGridViewCellEventArgs cellEvent)
{
    if (DataGridView1.Columns[cellEvent.ColumnIndex] is
        DataGridViewLinkColumn &&
        cellEvent.RowIndex != -1)
    { return true; }
    else { return false; }
}

private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
{
    if (DataGridView1.Columns[cellEvent.ColumnIndex] is
        DataGridViewButtonColumn &&
        cellEvent.RowIndex != -1)
    { return true; }
    else { return (false); }
}

private DataGridViewCell RetrieveSuperiorsLastNameCell(string employeeId)
{

    foreach (DataGridViewRow row in DataGridView1.Rows)
    {
        if (row.IsNewRow) { return null; }
        if (row.Cells[ColumnName.EmployeeId.ToString()].Value.ToString().Equals(employeeId))
        {
            return row.Cells[ColumnName.LastName.ToString()];
        }
    }
    return null;
}
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ