I am guessing from your last comment, that when you click the button, the text box will display the next value in the next row. If this is the case, then you will need to make a global variable to keep track of the next row index into the grid. Example, when the program starts, this buttonIndex will be zero (0), after the user clicks the button, then buttonIndex would be one (1), and so on. If buttonIndex’s value equals the last row index in the grid, then we need to reset buttonIndexs value to zero (0) to re-start at the top first row.
A few issues are not clear. For example, assume the current text in the text box is displaying the value from row four (4) in the grid. Then the user “deletes” this row four (4). The text box will not change and will continue to display the value from the now deleted row four. Then, if the user clicks the button… since the button index is at row four (4), the next index would be five (5), therefore the code will skip a row. In this example the initial row 5 (before row 4 was deleted) will be skipped.
If the user is allowed to delete rows, then the buttonIndex will have to be adjusted accordingly. If you do not address this, then the text box could display text that is not in the grid. Worst it will throw off the current buttonIndex and crash.
The above issues are something you will have to address. Below is code that will loop through the rows as per your requirement, however, there is no updating of the buttonIndex if a row(s) are deleted. This means the user could delete the current row that is displayed in text box and the text box will not change until the user clicks the button and could possibly skip a row. There are several checks though to keep the buttonIndex in range of the existing rows even if the user deletes rows. I hope that makes sense.
Are you sure, you do not want the text box to update if the user changes a row selection?
Code description
A global variable buttonIndex is used to keep a row index to the grid. Note: the text in the grid at row buttonIndex and the current text in the text box, MAY be different if the user deletes one or more rows. Therefore, checks are made to keep the buttonIndex within the bounds of the grids rows.
int buttonIndex = 0;
private void button7_Click(object sender, EventArgs e) {
if (buttonIndex >= dataGridView1.Rows.Count || dataGridView1.Rows[buttonIndex].IsNewRow)
buttonIndex = 0;
if (dataGridView1.AllowUserToAddRows == true) {
if (dataGridView1.RowCount > 1)
textBox1.Text = dataGridView1.Rows[buttonIndex].Cells["Columnname"].Value.ToString();
else
textBox1.Text = "";
} else {
if (dataGridView1.RowCount > 0)
textBox1.Text = dataGridView1.Rows[buttonIndex].Cells["Columnname"].Value.ToString();
else
textBox1.Text = "";
}
buttonIndex++;
}
DataGridView’sAllowUserToAddRowsproperty is set totrue, you will get this error. If you want to allow users to add rows, then you will need to check for this “New Row”. In addition, the code appears to be replacing the text in the text box with each iteration of the loop. This will result in the text box containing the text from only the last row in the grid. You may want to clarify what you want this button to do. - JohnGtextBox1.Text = dataGridView1.Rows[rowscount - 1].Cells["Columnname"].Value.ToString();- JohnG