1

DataGridView考虑到DataGridView充满数据并且当我自动单击一行数据时我想检索所有数据时,我应该使用什么事件。我尝试使用该事件CellContentClick,但它仅在我选择列数据而不是行时才被激活

private void dtSearch_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
4

3 回答 3

1

怎么样RowHeaderMouseClick 。

于 2012-11-20T00:49:37.557 回答
1

尝试使用 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);
        }
    }
于 2012-11-20T01:25:56.583 回答
1

我已经使用了以下效果。我处理MouseDown事件DataGridView并将整行设置为突出显示,以便很明显它已被选中(当然,除非您已经选择了整行)。

    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
            }
        }
    }
于 2012-11-20T00:36:19.387 回答