![](/img/trans.png)
[英]DataGridView selecting a specific row and retrieving all column values to an object array
[英]DataGridView selecting a specific row and retrieving its values
考慮到DataGridView
充滿了數據,我將在DataGridView
使用什么事件,當我自動單擊一行數據時,我想要檢索所有數據。 我已經嘗試使用事件CellContentClick
但它只在我選擇列數據而不是行時激活
private void dtSearch_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
我使用以下效果很好。 我處理DataGridView
的MouseDown
事件並設置要突出顯示的完整行,以便明顯它已被選中(除非你已經選擇了完整的行)。
private void dtSearch_MouseDown(object sender, MouseEventArgs e)
{
// Get the cell that was clicked from the location of the mouse pointer
DataGridView.HitTestInfo htiSelectedCell = dtSearch.HitTest(e.X, e.Y);
if (e.Button == MouseButtons.Left)
{
// Make sure that a cell was clicked, and not the column or row headers
// or the empty area outside the cells. If it is a cell,
// then select the entire row, set the current cell (to move the arrow to
// the current row)
//if (htiSelectedCell.Type == DataGridViewHitTestType.Cell)
if (htiSelectedCell.Type == DataGridViewHitTestType.RowHeader)
{
// do stuff here
}
}
}
嘗試使用CellClick事件,並遍歷檢索所需行值的列:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
}
public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
List<object> values = new List<object>();
int cols = this.dataGridView1.Columns.Count;
for (int col = 0; col < cols; col++)
{
values.Add(this.dataGridView1[col, e.RowIndex].Value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.