none
how to find selected cell index in datagridview

    Question

  • i have datagridview vth3 columns, how to find the cell index where the cursor is pointed..im using it for sopy paste application from excel to datagridview..wat ever data im pasting its getting pasted in 1st cell of selected row, i need to paste the data in which ever cell i select...
    Monday, February 13, 2012 11:54 AM

Answers

  • Hi we neeth Welcomes you to MSDn forums..

    It is very simple one..

    Use this code:

    private void button1_Click(object sender, EventArgs e)
            {
                int rPos = dataGridView1.CurrentCell.ColumnIndex;
                int i= dataGridView1.CurrentRow .Index ;
                dataGridView1.Rows[i].Cells[rPos].Value = "My New Value";         
               
            }

    I hope it will helps to you

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Monday, February 13, 2012 12:18 PM
  • Hi we neeth,
    How is it going with Jo Swa’s suggestion?
    You can get the selected cell by using DataGridView.CurrentCell property.
    You can paste the value in the DataGridView cell by the following code.

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            if (this.dataGridView1.CurrentCell != null)
            {
                this.dataGridView1.CurrentCell.Value = Clipboard.GetText();
            }
        }
    }

    However, I’m a little confused about your description. What you do mean by “wat ever data im pasting its getting pasted in 1st cell of selected row”? By default, the DataGridView control will not paste the value into the cell if you just select the cell. Could you please show us the code?
    Anyway, if you want to paste the value “where the cursor is pointed”, then you can try the code below.

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            Point cursorPos = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
            DataGridView.HitTestInfo hit = this.dataGridView1.HitTest(cursorPos.X, cursorPos.Y);
            if (hit.RowIndex > -1 && hit.ColumnIndex > -1)
            {
                // check if it is new row
                if (hit.RowIndex == this.dataGridView1.RowCount - 1)
                {
                    this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex].Value = Clipboard.GetText();
                }
                else
                {
                    this.dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex].Value = Clipboard.GetText();
                }
            }
        }
    }

    If there is anything unclear, please let us know.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, February 15, 2012 5:19 AM

All replies

  • Hi we neeth Welcomes you to MSDn forums..

    It is very simple one..

    Use this code:

    private void button1_Click(object sender, EventArgs e)
            {
                int rPos = dataGridView1.CurrentCell.ColumnIndex;
                int i= dataGridView1.CurrentRow .Index ;
                dataGridView1.Rows[i].Cells[rPos].Value = "My New Value";         
               
            }

    I hope it will helps to you

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Monday, February 13, 2012 12:18 PM
  • Hi we neeth,
    How is it going with Jo Swa’s suggestion?
    You can get the selected cell by using DataGridView.CurrentCell property.
    You can paste the value in the DataGridView cell by the following code.

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            if (this.dataGridView1.CurrentCell != null)
            {
                this.dataGridView1.CurrentCell.Value = Clipboard.GetText();
            }
        }
    }

    However, I’m a little confused about your description. What you do mean by “wat ever data im pasting its getting pasted in 1st cell of selected row”? By default, the DataGridView control will not paste the value into the cell if you just select the cell. Could you please show us the code?
    Anyway, if you want to paste the value “where the cursor is pointed”, then you can try the code below.

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            Point cursorPos = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
            DataGridView.HitTestInfo hit = this.dataGridView1.HitTest(cursorPos.X, cursorPos.Y);
            if (hit.RowIndex > -1 && hit.ColumnIndex > -1)
            {
                // check if it is new row
                if (hit.RowIndex == this.dataGridView1.RowCount - 1)
                {
                    this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex].Value = Clipboard.GetText();
                }
                else
                {
                    this.dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex].Value = Clipboard.GetText();
                }
            }
        }
    }

    If there is anything unclear, please let us know.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, February 15, 2012 5:19 AM