Anonymous261977

I want to display some data on the row selection of datagridview (windows .Net application).

User may select the row using the keyup and down .

I can not get the RowIndex on the keyup or keydown event of datagridview

pl suggest



Re: Windows Forms General SelectedRow in DataGridView

Fábio

This code will get the data of the first cell in the selected row:

Code Block

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)

{

TextBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

int myIndex = dataGridView1.SelectedRows[0].Index; //This only gets the index of the selected Row

}

Hope this helps,

Regarads,

Fabio






Re: Windows Forms General SelectedRow in DataGridView

Anonymous261977

thanks for reply.

But I do not wish to hard code the index of row as u specified in your reply.

My requirement is user will select the row using key board up and down arrow key and I want to display value of two columns of the datagridview in the text boxes.

please suggest.





Re: Windows Forms General SelectedRow in DataGridView

Fábio

The Key events of the DataGridView does not provide any information about the current row index, in fact the selected row

can be one or can be many, depending on the user selection or your datagrid restriction to multi-select. If you want to get

the values of these columns the only way is through the index, and you get that by using the SelectedRow collection.

There is no need to specifically use the key down and up.

And it will get the values of columns 1 and 2 of the current selected row.

Note: Don't be fooled by the fact that I specify the index of the SelectedRows. This means that there may

be more than one selected row, and this would be the first selected row, which will be the one highlighted on the datagrid.

Code Block

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)

{

TextBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

TextBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();

}

If this still isn't what you need. Please be more specific and give more detail of what you are trying to acomplish





Re: Windows Forms General SelectedRow in DataGridView

Anonymous261977

Thanks for reply again.


Sorry I mentioned about the keyup/down event in my initial post. But my second post contains the exact requirement.

Please suggest me any DataGridView event which can accomplish:

My requirement is user will select the row using keyboard up and down arrow key and I want to display value of two columns of the datagridview in the text boxes.

thanks





Re: Windows Forms General SelectedRow in DataGridView

Anonymous261977

Thanks for reply again.


Sorry I mentioned about the keyup/down event in my initial post. But my second post contains the exact requirement.

Please suggest me any DataGridView event which can accomplish my below requirement.

User will select the row of the DataGridView using the keyboard up/down arrow key. And I want to display the value of the two columns of the selected row in the textboxes.





Re: Windows Forms General SelectedRow in DataGridView

Fábio

that is exactly what the code I posted will do






Re: Windows Forms General SelectedRow in DataGridView

Anonymous261977

Thanks for reply again.


Sorry I mentioned about the keyup/down event in my initial post. But my second post contains the exact requirement.

Please suggest me any DataGridView event which can accomplish my below requirement.

User will select the row of the DataGridView using the keyboard up/down arrow key. And I want to display the value of the two columns of the selected row in the textboxes.





Re: Windows Forms General SelectedRow in DataGridView

Fábio

Honestly, I don't know why that code won't work for you, and if you keep copying and pasting your post it is you

the one that will not have your issue solved. I'm trying to help on your problem, not mine.

Also the ArrowUp and ArrowDown keys don't throw the keypress event. The only way to do that

is to inherit the DataGridView class and override the ProcessCmdKey function.






Re: Windows Forms General SelectedRow in DataGridView

Anonymous261977

I apologies again.

My internet is not working properly. Hence by mistake I sent post twice.

And in the meantime, you replied for the question. I didn't know that post is already in.

thanks for your help.





Re: Windows Forms General SelectedRow in DataGridView

JRQ

Anonymous261977 wrote:

thanks for reply.

But I do not wish to hard code the index of row as u specified in your reply.

My requirement is user will select the row using key board up and down arrow key and I want to display value of two columns of the datagridview in the text boxes.

please suggest.

SelectedRows is a collection of selected rows. In your case, it will only have one entry. The currently selected row.





Re: Windows Forms General SelectedRow in DataGridView

Anonymous261977

Fabio

Thanks for ur help.

I am using the Keyup event and getting the values fine. But using the Keydown event, I am getting the cell value of previous row. Can you please help

I wish to use both keyup and keydown event.





Re: Windows Forms General SelectedRow in DataGridView

Fábio

That happens because when the keydown event is called, the current selected cell is still the previous cell. It happens this way:

You press the arrow Down

// Key events starts

-KeyDown

First Cell

Second Cell

// DataGridView changes the selected cell now

-KeyUp

First Cell

Second Cell

-KeyPress

First Cell

Second Cell

A workaroung to this is add +1 in the row index on the keydown event. But be careful as if it is the last cell that

is selected, it will throw an exception






Re: Windows Forms General SelectedRow in DataGridView

Anonymous261977

thanks Fabio