![](/img/trans.png)
[英]Windows Forms dataGridView - how to set rows enable on click event
[英]How to enable double click function on DataGridView in C# Windows Forms
我不知道當用戶單擊具有五列的行以在消息框上顯示具有五列的這一整行的值時,如何啟用雙擊功能?
我使用MySQL db。
我有這種方法:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
{
return;
}
int index = e.RowIndex;
dataGridView1.Rows[index].Selected = true;
}
我創建了這個方法..
private void allOrders_DoubleClick(object sender, EventArgs e)
{
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.MultiSelect = false;
}
問題是 ? 如何在MessageBox中顯示包含所有列的行?
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
}
這是通過DataGridView事件完成的。 在DataGridView上雙擊時,將執行以下上面的代碼。
-您可以獲得各種活動
1-單擊DataGridView控件
2-在屬性窗口中,單擊事件選項卡,現在您可以在那里獲取所有事件。
快樂編碼
假設您已經從DataTable
綁定了DataGridView
的數據源。 例如:
private void InitDataGridView()
{
DataTable table = new DataTable();
table.Columns.Add("1");
table.Columns.Add("2");
table.Columns.Add("3");
table.Columns.Add("4");
table.Columns.Add("5");
table.Rows.Add("1", 2, 3, "4", 5);
table.Rows.Add("11", 21, 31, "41", 51);
table.Rows.Add("11", 12, 13, "14", 15);
table.Rows.Add("21", 22, 23, "24", 25);
table.Rows.Add("13", 32, 33, "34", 35);
}
dataGridView1.DataSource = table;
現在讓我們看一下DoubleClick()
方法,例如,您可以將選定的行集合放入字符串數組,然后將其傳遞到MessageBox
或新Form
或在需要的地方使用它:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow selectedRow = dataGridView1.Rows[e.RowIndex]; //Get selected Row
string[] selectedValues = new string[selectedRow.Cells.Count]; //Init string array or list, or custom object array/list
for (int i = 0; i < selectedRow.Cells.Count; i++)
{
selectedValues[i] = selectedRow.Cells[i].Value.ToString(); //Fill your array with cell values
}
//Your next code goes here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.