Display before any row using "Button"

Israel

Active member
Joined
Jan 10, 2020
Messages
26
Programming Experience
Beginner
Hi,
I was writing these codes to display the before any row. For example:
Samia
Alam
Shell

Then when I click on "Shell" its display on textbox "Alam". If I click on "Alam" its display "Samia".
These codes work perfetly using the datagrid. How can I do using Button.

C#:
private void DGVTest_CellClick(object sender, EventArgs e)
{
if (e.RowIndex == 0)

{
 lblID.Text = string.empty;
 txtName.Text = string.empty;
}
else if (e.RowIndex >= 1)

{

DataGridViewRow row = this.dgvTest.Rows[e.RowIndex - 1];
 if (row != null)
 {

 lblID.Text = row.Cells["id"].Value.ToString();

 txtName.Text = row.Cells["name"].Value.ToString();
}
}
 
What do you mean by:
How can I do using Button

Is your intent to create a data grid with one column filled with buttons instead of plain old cells, you could use a DataGridViewButtonColumn for that column.

If your intent is just to get rid of the data grid view, and simply have a button?
 
What do you mean by:


Is your intent to create a data grid with one column filled with buttons instead of plain old cells, you could use a DataGridViewButtonColumn for that column.

If your intent is just to get rid of the data grid view, and simply have a button?
Thanx to respond to me.
But my intent is just to display the row before any row. Why? Just because I already wrote others codes that will target on the first row after a filter for example. Then its will display the before one.
 
So you really need to have another button on your form not to replace the grid, nor to add a column of buttons. In that case you just add a button to your form, and then in that button's click handler look at DataGridView.CurrentRow to figure out the current row the user is currently on. Subtract one from that row like you are doing in your code above to determine what the row above it is.
 
Or better yet, instead of having a button that the user has to click on, why not just register for DataGridView.RowEnter event? Each time the user changes which row they are on, you can update your label and textboxes with the information of the row about the row that is being entered.
 
I am
Or better yet, instead of having a button that the user has to click on, why not just register for DataGridView.RowEnter event? Each time the user changes which row they are on, you can update your label and textboxes with the information of the row about the row that is being entered.
Can you correct my codes please. I would like to see these codes working as i am expecting to...
 
I don't know about this particular forum, but most programming forums are not code writing services. I suggest following the link that I put in that reply and looking at the sample code presented there to see how you can use that event.
 
Make an attempt to implement the suggestion if you found it useful. If you're still facing problems. Report those problems, and once it has been seen that you too are making an effort to resolve your own issues, maybe someone will be kind enough to oblige changing your code for you. Its generally expected if you ask someone to write code for you, that you plan to pay their going rates for their services. If you don't have any desires to pay someone for their work, then it is morally wrong to ask someone to work for free. Wouldn't you agree?

But my intent is just to display the row before any row. Why? Just because I already wrote others codes that will target on the first row after a filter for example. Then its will display the before one.
It sounds like you need a class. And it also sounds like you should be returning all your results to individual lists and iterating over them to match them accordingly before doing anything with your DGV/UI. You could make it easy if you take advantage of object orientating rules. There are some well written rules wrote here which you should follow : 10 Golden Rules Of Good OOP for writing good code!
 
Back
Top Bottom