DataGridViewCellContextMenuStripNeededEventArgs Klasa
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Udostępnia dane dla zdarzenia CellContextMenuStripNeeded.
public ref class DataGridViewCellContextMenuStripNeededEventArgs : System::Windows::Forms::DataGridViewCellEventArgs
public class DataGridViewCellContextMenuStripNeededEventArgs : System.Windows.Forms.DataGridViewCellEventArgs
type DataGridViewCellContextMenuStripNeededEventArgs = class
inherit DataGridViewCellEventArgs
Public Class DataGridViewCellContextMenuStripNeededEventArgs
Inherits DataGridViewCellEventArgs
- Dziedziczenie
Przykłady
W poniższym przykładzie kodu użyto DataGridViewCellContextMenuStripNeededEventArgs klasy , aby ustawić menu skrótów bez udostępniania wiersza.
ToolStripMenuItem^ wholeTable;
ToolStripMenuItem^ lookUp;
System::Windows::Forms::ContextMenuStrip^ strip;
String^ cellErrorText;
void dataGridView1_CellContextMenuStripNeeded( Object^ /*sender*/,
DataGridViewCellContextMenuStripNeededEventArgs^ e )
{
cellErrorText = String::Empty;
if ( strip == nullptr )
{
strip = gcnew System::Windows::Forms::ContextMenuStrip;
lookUp->Text = L"Look Up";
wholeTable->Text = L"See Whole Table";
strip->Items->Add( lookUp );
strip->Items->Add( wholeTable );
}
e->ContextMenuStrip = strip;
}
void wholeTable_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
dataGridView1->DataSource = Populate( L"Select * from employees", true );
}
DataGridViewCellEventArgs^ theCellImHoveringOver;
void dataGridView1_CellMouseEnter( Object^ /*sender*/, DataGridViewCellEventArgs^ e )
{
theCellImHoveringOver = e;
}
DataGridViewCellEventArgs^ cellErrorLocation;
void lookUp_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
try
{
dataGridView1->DataSource = Populate( String::Format( L"Select * from employees where {0} = '{1}'", dataGridView1->Columns[ theCellImHoveringOver->ColumnIndex ]->Name, dataGridView1->Rows[ theCellImHoveringOver->RowIndex ]->Cells[ theCellImHoveringOver->ColumnIndex ]->Value ), true );
}
catch ( ... )
{
cellErrorText = L"Can't look this cell up";
cellErrorLocation = theCellImHoveringOver;
}
}
void dataGridView1_CellErrorTextNeeded( Object^ /*sender*/, DataGridViewCellErrorTextNeededEventArgs^ e )
{
if ( cellErrorLocation != nullptr )
{
if ( e->ColumnIndex == cellErrorLocation->ColumnIndex && e->RowIndex == cellErrorLocation->RowIndex )
{
e->ErrorText = cellErrorText;
}
}
}
DataTable^ Populate( String^ query, bool resetUnsharedCounter )
{
if ( resetUnsharedCounter )
{
ResetCounter();
}
// Alter the data source as necessary
SqlDataAdapter^ adapter = gcnew SqlDataAdapter( query,
gcnew SqlConnection( L"Integrated Security=SSPI;Persist Security Info=False;"
L"Initial Catalog=Northwind;Data Source= localhost" ) );
DataTable^ table = gcnew DataTable;
adapter->Fill( table );
return table;
}
Label^ count;
int unsharedRowCounter;
void ResetCounter()
{
unsharedRowCounter = 0;
count->Text = unsharedRowCounter.ToString();
}
private ToolStripMenuItem wholeTable = new ToolStripMenuItem();
private ToolStripMenuItem lookUp = new ToolStripMenuItem();
private ContextMenuStrip strip;
private string cellErrorText;
private void dataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
cellErrorText = String.Empty;
if (strip == null)
{
strip = new ContextMenuStrip();
lookUp.Text = "Look Up";
wholeTable.Text = "See Whole Table";
strip.Items.Add(lookUp);
strip.Items.Add(wholeTable);
}
e.ContextMenuStrip = strip;
}
private void wholeTable_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = Populate("Select * from employees", true);
}
private DataGridViewCellEventArgs theCellImHoveringOver;
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
theCellImHoveringOver = e;
}
private DataGridViewCellEventArgs cellErrorLocation;
private void lookUp_Click(object sender, EventArgs e)
{
try
{
dataGridView1.DataSource = Populate("Select * from employees where " +
dataGridView1.Columns[theCellImHoveringOver.ColumnIndex].Name + " = '" +
dataGridView1.Rows[theCellImHoveringOver.RowIndex].
Cells[theCellImHoveringOver.ColumnIndex].Value + "'",
true);
}
catch (SqlException)
{
cellErrorText = "Can't look this cell up";
cellErrorLocation = theCellImHoveringOver;
}
}
private void dataGridView1_CellErrorTextNeeded(object sender,
DataGridViewCellErrorTextNeededEventArgs e)
{
if (cellErrorLocation != null)
{
if (e.ColumnIndex == cellErrorLocation.ColumnIndex &&
e.RowIndex == cellErrorLocation.RowIndex)
{
e.ErrorText = cellErrorText;
}
}
}
private DataTable Populate(string query, bool resetUnsharedCounter)
{
if (resetUnsharedCounter)
{
ResetCounter();
}
// Alter the data source as necessary
SqlDataAdapter adapter = new SqlDataAdapter(query,
new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost"));
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
return table;
}
private Label count = new Label();
private int unsharedRowCounter;
private void ResetCounter()
{
unsharedRowCounter = 0;
count.Text = unsharedRowCounter.ToString();
}
Private WithEvents wholeTable As New ToolStripMenuItem()
Private WithEvents lookUp As New ToolStripMenuItem()
Private strip As ContextMenuStrip
Private cellErrorText As String
Private Sub dataGridView1_CellContextMenuStripNeeded(ByVal sender As Object, _
ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _
Handles dataGridView1.CellContextMenuStripNeeded
cellErrorText = String.Empty
If strip Is Nothing Then
strip = New ContextMenuStrip()
lookUp.Text = "Look Up"
wholeTable.Text = "See Whole Table"
strip.Items.Add(lookUp)
strip.Items.Add(wholeTable)
End If
e.ContextMenuStrip = strip
End Sub
Private Sub wholeTable_Click(ByVal sender As Object, ByVal e As EventArgs) Handles wholeTable.Click
dataGridView1.DataSource = Populate("Select * from employees", True)
End Sub
Private theCellImHoveringOver As DataGridViewCellEventArgs
Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dataGridView1.CellMouseEnter
theCellImHoveringOver = e
End Sub
Private cellErrorLocation As DataGridViewCellEventArgs
Private Sub lookUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lookUp.Click
Try
dataGridView1.DataSource = Populate("Select * from employees where " & _
dataGridView1.Columns(theCellImHoveringOver.ColumnIndex).Name & " = '" & _
dataGridView1.Rows(theCellImHoveringOver.RowIndex).Cells(theCellImHoveringOver.ColumnIndex).Value.ToString() & _
"'", True)
Catch ex As SqlException
cellErrorText = "Can't look this cell up"
cellErrorLocation = theCellImHoveringOver
End Try
End Sub
Private Sub dataGridView1_CellErrorTextNeeded(ByVal sender As Object, _
ByVal e As DataGridViewCellErrorTextNeededEventArgs) _
Handles dataGridView1.CellErrorTextNeeded
If (Not cellErrorLocation Is Nothing) Then
If e.ColumnIndex = cellErrorLocation.ColumnIndex AndAlso _
e.RowIndex = cellErrorLocation.RowIndex Then
e.ErrorText = cellErrorText
End If
End If
End Sub
Private Function Populate(ByVal query As String, ByVal resetUnsharedCounter As Boolean) As DataTable
If resetUnsharedCounter Then
ResetCounter()
End If
' Alter the data source as necessary
Dim adapter As New SqlDataAdapter(query, _
New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" & _
"Initial Catalog=Northwind;Data Source=localhost"))
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
adapter.Fill(table)
Return table
End Function
Private count As New Label()
Private unsharedRowCounter As Integer
Private Sub ResetCounter()
unsharedRowCounter = 0
count.Text = unsharedRowCounter.ToString()
End Sub
Uwagi
Zdarzenie CellContextMenuStripNeeded występuje tylko wtedy, gdy właściwość kontrolki DataGridViewDataSource jest ustawiona lub jej VirtualMode właściwość to true.
Po obsłużeniu CellContextMenuStripNeeded zdarzenia menu skrótów określone w procedurze obsługi jest wyświetlane za każdym razem, gdy użytkownik kliknie komórkę prawym przyciskiem myszy. Jest to przydatne, gdy chcesz wyświetlić menu skrótów określone przez bieżący stan lub wartość komórki.
Zdarzenie CellContextMenuStripNeeded występuje również za każdym razem, gdy wartość DataGridViewCell.ContextMenuStrip właściwości zostanie pobrana programowo lub gdy użytkownik kliknie komórkę prawym przyciskiem myszy.
Możesz użyć ColumnIndex właściwości i RowIndex , aby określić stan lub wartość komórki, a następnie użyć tych informacji do ustawienia ContextMenuStrip właściwości. Ta właściwość jest inicjowana z wartością właściwości komórki ContextMenuStrip , która zastępuje wartość zdarzenia.
CellContextMenuStripNeeded Obsłuż zdarzenie podczas pracy z dużą ilością danych, aby uniknąć kar za wydajność ustawiania wartości komórki ContextMenuStrip dla wielu komórek. Aby uzyskać więcej informacji, zobacz Best Practices for Scaling the Windows Forms DataGridView Control (Najlepsze rozwiązania dotyczące skalowania kontrolki DataGridView formularzy systemu Windows).
Można również określić menu skrótów dla poszczególnych wierszy, a nie poszczególnych komórek, ustawiając właściwość wiersza ContextMenuStrip lub obsługując DataGridView zdarzenie kontrolki RowContextMenuStripNeeded . Ustawienie właściwości komórki ContextMenuStrip zastępuje ustawienie właściwości wiersza ContextMenuStrip , a CellContextMenuStripNeeded zdarzenie zastępuje zarówno zdarzenie, jak RowContextMenuStripNeeded i ustawienie właściwości wiersza ContextMenuStrip . Można jednak określić null menu skrótów komórek, aby uniemożliwić zastąpienie menu skrótów wierszy.
Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.
Konstruktory
| Nazwa | Opis |
|---|---|
| DataGridViewCellContextMenuStripNeededEventArgs(Int32, Int32) |
Inicjuje nowe wystąpienie klasy DataGridViewCellContextMenuStripNeededEventArgs. |
Właściwości
| Nazwa | Opis |
|---|---|
| ColumnIndex |
Pobiera wartość wskazującą indeks kolumn komórki, dla którego występuje zdarzenie. (Odziedziczone po DataGridViewCellEventArgs) |
| ContextMenuStrip |
Pobiera lub ustawia menu skrótów dla komórki, która wywołała CellContextMenuStripNeeded zdarzenie. |
| RowIndex |
Pobiera wartość wskazującą indeks wiersza komórki, dla którego występuje zdarzenie. (Odziedziczone po DataGridViewCellEventArgs) |
Metody
| Nazwa | Opis |
|---|---|
| Equals(Object) |
Określa, czy określony obiekt jest równy bieżącemu obiektowi. (Odziedziczone po Object) |
| GetHashCode() |
Służy jako domyślna funkcja skrótu. (Odziedziczone po Object) |
| GetType() |
Pobiera Type bieżącego wystąpienia. (Odziedziczone po Object) |
| MemberwiseClone() |
Tworzy płytkią kopię bieżącego Object. (Odziedziczone po Object) |
| ToString() |
Zwraca ciąg reprezentujący bieżący obiekt. (Odziedziczone po Object) |
Dotyczy
Zobacz też
- DataGridView
- CellContextMenuStripNeeded
- DataSource
- VirtualMode
- OnCellContextMenuStripNeeded(DataGridViewCellContextMenuStripNeededEventArgs)
- DataGridViewCellContextMenuStripNeededEventHandler
- ContextMenuStrip
- ContextMenuStrip
- RowContextMenuStripNeeded
- ContextMenuStrip
- ContextMenuStrip
- Najlepsze rozwiązania dotyczące skalowania kontrolki DataGridView formularzy systemu Windows