4

I used following code to count datagrid rows, but it works on button only. I want the total number of rows in a textbox, but it doesn't increment itself as the number of rows increase.

int numRows = dataGridView1.Rows.Count;

txtTotalItem.Text = numRows.ToString();
3
  • 4
    So you want the number in the textbox to change automatically when rows are added/removed rather than when you click a button? Just use the RowsAdded and RowsRemoved events to change the textbox. Commented Dec 18, 2015 at 16:57
  • should i use RowsAdded or RowsRemoved in textchanged property of textbox? Commented Dec 18, 2015 at 16:59
  • its Access database Commented Dec 18, 2015 at 17:24

3 Answers 3

12

When you assign the number, the textbox will display only that number, it will not update.

You can use an event and assign the new number to the textbox.

dataGridView1.RowsAdded+=(s,a)=>OnRowNumberChanged;

private void OnRowNumberChanged()
{
    txtTotalItem.Text = dataGridView1.Rows.Count.ToString();
}

Following Equalsk's answer, you need to have the event for removed rows as well. You can subscribe to both events with the same function since you need to just check the number.

dataGridView1.RowsRemoved+=(s,a)=>OnRowNumberChanged();
Sign up to request clarification or add additional context in comments.

1 Comment

Upvote for subscribing to both at the same time, much neater than mine
4

If I've understood your question correctly you want the TextBox value to change automatically when rows are added or removed because at the moment you have to press a button to refresh. Am I right?

If so just subscribe to the .RowsAdded and .RowsRemoved events and update the TextBox from there.

dataGridView1.RowsAdded += RowsAdded;
dataGridView1.RowsRemoved += RowsRemoved;

private void RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    txtTotalItem.Text = dataGridView1.Rows.Count.ToString();
}

private void RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
    txtTotalItem.Text = dataGridView1.Rows.Count.ToString();
}

3 Comments

all answers works fine but following code was generating error. and after removing that line. rest of code works as it supposed to be. dataGridView1.RowsAdded += RowsAdded;
why this code(dataGridView1.RowsRemoved+=dataGridView1_RowsRemoved;) was used because this( txtTotalItem.Text = dataGridView1.Rows.Count.ToString(); ) is still working even after removing that
Rows removed would update the text box if rows were removed. It might be that you never remove rows and therefore don't need it.
0

the datagridview will count the last empty row so make sure to

look for AllowUserToAddRows property of datagridview and set it to False.

Or add this to your code, in my case i used a label and i named it labelTotalResult, do this if you want to keep the property as true

                    labelTotalResult.Text = (dataGridView1.RowCount - 1).ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.