0

I have a datagridview and a status strip on my form. When I click a row or cell, the label from the status strip should change according to it's row number. My datagridview is being generated at run time by a dataset.

3
Contributors
6
Replies
9
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
Featured Replies
  • 2
    ddanbe 2,525   5 Years Ago

    Many Click events of the DataGrridView show a DataGridViewCellEventArgs [CODE=c#]private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { // e.RowIndex and e.ColumnIndex }[/CODE] This class gives you access to the row and column. Read More

  • I will only add to ddanbe` post: If you are not in some dgv`s event handler you get the row number like: [CODE] private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { int rowIndex1 = e.RowIndex; int rowIndex2 = GetRowIndex(); } private int GetRowIndex() { int rowIndex = this.dataGridView1.CurrentCell.RowIndex; return rowIndex; } … Read More

  • Yes you do: [CODE] if(dataGridView1.Rows.Count > 0) { //your code }[/CODE] Read More

2

Many Click events of the DataGrridView show a DataGridViewCellEventArgs

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // e.RowIndex and e.ColumnIndex
        }

This class gives you access to the row and column.

Comments
thanks
You are welcome :)
1

I will only add to ddanbe` post:
If you are not in some dgv`s event handler you get the row number like:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex1 = e.RowIndex;
            int rowIndex2 = GetRowIndex();
        }

        private int GetRowIndex()
        {
            int rowIndex = this.dataGridView1.CurrentCell.RowIndex;
            return rowIndex;
        }

Mitja

Comments
Thanks for the addition!
0

It begins with 0 for the first row, so does that mean I just need to add 1?

0

Depends on what your intentions are, as long as you know what you are doing, please add 1. No problem.

0

There's an error if I click an empty datagridview. I suppose I need to set a condition for this? Like if datagridview is not null?

I need to indicate the row is 0, so I think an if else condition is necessary, don't you think? But using condition just to tell the row is 0? Isn't the code supposed to do that?

Edited by AngelicOne: n/a

This question has already been answered. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.