0

I am working on a Windows application where I get an input from a TextBox and add it to the DataGridView when user clicks on a Button (Add).

enter image description here

My problem is that when I add the text for the first time, it works fine and its added to the grid.
When I add new text, it's not added to the DataGridView. Once the Form is closed and reopened with the same object then I am able to see it.

Code:

private void btnAddInput_Click(object sender, EventArgs e)
{
    if (Data == null)
        Data = new List<Inputs>();

    if (!string.IsNullOrWhiteSpace(txtInput.Text))
    {
        Data.Insert(Data.Count, new Inputs()
        {
            Name = txtInput.Text,
            Value = string.Empty
        });
    }
    else
    {
        MessageBox.Show("Please enter parameter value", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    txtInput.Text = "";
    gridViewInputs.DataSource = Data;
}

I am not sure what is causing the record not to be added to grid on second add button click.

3
  • Are you getting any error? Commented Jan 17, 2019 at 10:04
  • Try using gridViewInputs.DisplayMember = "Name";. Commented Jan 17, 2019 at 10:10
  • Why are you using Data.Insert(), when you don't need to keep track of the index ? I would use simple Data.Add() - It would also have performance advantage when having a very populated List, and when removing elements. Commented Jan 17, 2019 at 10:12

1 Answer 1

1

You could set the DataGridView.DataSource to null before setting a new one.
This would cause the DataGridView to refresh its content with the new data in the source List<Inputs>:
the underlying DataGridViewDataConnection is reset only when the DataSource reference is different from the current or is set to null.

Note that when you reset the DataSource, the RowsRemoved event is raised multiple times (once for each row removed).

I suggest to change the List to a BindingList, because any change to the List will be reflected automatically and because it will allow to remove rows from the DataGridView if/when required: using a List<T> as DataSource will not allow to remove a row.

BindingList<Inputs> InputData = new BindingList<Inputs>();

You can always set the AllowUserToDeleteRows and AllowUserToAddRows properties to false if you don't want your Users to tamper with the grid content.

For example:

public class Inputs
{
    public string Name { get; set; }
    public string Value { get; set; }
}

internal BindingList<Inputs> InputData = new BindingList<Inputs>();

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.DataSource = InputData;
}

private void btnAddInput_Click(object sender, EventArgs e)
{
    string textValue = txtInput.Text.Trim();
    if (!string.IsNullOrEmpty(textValue))
    {
        InputData.Add(new Inputs() {
            Name = textValue,
            Value = "[Whatever this is]"
        });
        txtInput.Text = "";
    }
    else
    {
        MessageBox.Show("Not a valid value");
    }
}

If you want to keep using a List<T>, add the code required to reset the DataGridView.DataSource:

private void btnAddInput_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(textValue))
    {
        //(...)
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = InputData;
        txtInput.Text = "";
    }
    //(...)
Sign up to request clarification or add additional context in comments.

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.