DataGridView选择特定行并检索其值

DataGridView选择特定行并检索其值

本文介绍了DataGridView选择特定行并检索其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到 DataGridView 的数据已满,当我点击一个数据时,我将在 DataGridView 中使用什么事件我想要检索所有数据的行数据。我尝试使用事件 CellContentClick ,但只有当我选择列数据而不是行时激活

  private void dtSearch_CellContentClick(object sender,DataGridViewCellEventArgs e)
{

}


解决方案

我已经使用了以下效果。我处理 DataGridView MouseDown 事件,并设置要突出显示的完整行,以便显然已经被选择(除非您已经选择了完整的行)

  private void dtSearch_MouseDown(object sender,MouseEventArgs e)
{
//获取从鼠标指针位置点击的单元格

DataGridView.HitTestInfo htiSelectedCell = dtSearch.HitTest(eX,eY);

if(e.Button == MouseButtons.Left)
{
//确保单元格被点击,而不是列或行标题
/ /或细胞外的空区域。如果它是一个单元格,
//然后选择整个行,设置当前单元格(将箭头移动到
//当前行)

// if( htiSelectedCell.Type == DataGridViewHitTestType.Cell)
if(htiSelectedCell.Type == DataGridViewHitTestType.RowHeader)
{
// do stuff here
}
}
}


What event shall I use in a DataGridView considering that the DataGridView is full of data and when I click a row of data automatically I would like to retrieve all the data. I've tried using the event CellContentClick but it is only activated when I select a column data instead of a row

private void dtSearch_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
解决方案

I have used the following to good effect. I handle the MouseDown event for the DataGridView and set the full row to be highlighted so that it is obvious it has been selected (unless of course you already have your full rows being selected).

    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
            }
        }
    }

这篇关于DataGridView选择特定行并检索其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:14