Arturo de Armas
Arturo de Armas

Reputation: 1

Read next row DataGridView each button click

I'm new in C#, learning by myself.

For learning purposes I'm trying to read the contents of the row for a particular column each time that I click on a button and display the value into a textbox. The DataGridView has around 10 rows and 10 columns. To do that, this is my code so far:

private void button7_Click(object sender, EventArgs e)
    {
        int rowscount = dataGridView1.Rows.Count;
        for (int i = 0; i < rowscount; i++)
        {
           textBox1.Text = 
dataGridView1.Rows[i].Cells["Columnname"].Value.ToString();

        }
     }

I tried to switch the line: dataGridView1.Rows[i].Cells["Columnname"].Value.ToString(); to dataGridView1.Rows[1].Cells["Columnname"].Value.ToString() and it properly reads the line 1 for the column "Columname" and place in the textBox1. But when I switch again to [i] the following error is displayed:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Any idea?

Upvotes: 0

Views: 1115

Answers (2)

adnan Alyaari
adnan Alyaari

Reputation: 61

must be set for condation from :

for (int i = 0; i < rowscount; i++)

to :

for (int i = 0; i < rowscount-1; i++)

The last empty row in datagridview must be deleted

Upvotes: 0

JohnG
JohnG

Reputation: 9469

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++;
}

Upvotes: 1

Related Questions