-2

I'm developing a basic Game Launcher and I'm running into an error as show below... the error states that "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

what can i do to resolve this issue?

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.Show();
}

CODE FROM ANOTHER FORM:

MainForm mainForm;
DataTable table;

    public Form2(MainForm mf)
    {
        InitializeComponent();
        this.mainForm = mf; 
    }


    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "Exe Files (.exe)|*.exe|All Files (*.*)|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            txtPath.Text = openFileDialog1.FileName;
        }
            

    }
    
    private void button2_Click(object sender, EventArgs e)
    {

        DataTable dataTable = (DataTable)dataGridView1.DataSource;
        DataRow drToAdd = dataTable.NewRow();

        drToAdd["Game"] = txtTitle;
        drToAdd["Path"] = txtPath;

        mainForm.dataGridView1.Rows.Add(drToAdd);
        dataTable.AcceptChanges();
        
    }

    private void AddGame_Load(object sender, EventArgs e)
    {
        table = new DataTable();
        table.Columns.Add("Game", typeof(string));
        table.Columns.Add("Path", typeof(string));
        dataGridView1.DataSource = table;
        dataGridView1.Columns["Path"].Visible = false;
        dataGridView1.Columns["Game"].Width = 138;
        dataGridView1.ScrollBars = ScrollBars.None;
    }
}

Photo of Error

Lachie
  • 5
  • 3
  • 1
    `Form2(MainForm mf)` this constructor of Form2 needs an object of `MainForm` while yo are passing empty string `""`. Empty string will not be magically converted to MainForm object. that's why you are getting this error. Where `button1_Click` code is written? – Chetan Jun 21 '22 at 04:28
  • removed the string and now im getting this error: There is no argument given that corresponds to the required formal parameter 'mf' of 'Form2.Form2(MainForm)' Any idea on how to fix this? – Lachie Jun 21 '22 at 04:55
  • You need to pass the MainForm object. If the button1_Click handler is in MainForm.cs, you can write `new Form2(this);` – Klaus Gütter Jun 21 '22 at 05:19
  • i have updated the question to another error, any ideas on how to fix this? – Lachie Jun 21 '22 at 08:42
  • The Form2 constructor has a parameter which you are not sending through in your code example. `Form2 frm2 = new Form2();` -> `Form2 frm2 = new Form2(this);` – string.Empty Jun 21 '22 at 08:45
  • Does this answer your question? [Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound](https://stackoverflow.com/questions/8708057/rows-cannot-be-programmatically-added-to-the-datagridviews-row-collection-when) – string.Empty Jun 21 '22 at 08:55

1 Answers1

0

Consider defining an event in the child form that passes intended data to the main form. In the following sample there are two columns pre-defined in the DataGridView with DataPropertyName set for each.

MainForm

  • Setup a hardwired DataTable in load event which is set to the DataGridView via a BindingSource (which is optional but makes life easier in many way).
  • In a button click event show the child form along with subscribing to an event for adding rows to the DataGridView.

Code

public partial class MainForm : Form
{
    private readonly BindingSource _bindingSource = 
        new BindingSource();

    public MainForm()
    {
        InitializeComponent();

        dataGridView1.AutoGenerateColumns = false;

        DataTable table = new DataTable();
        table.Columns.Add("FirstName", typeof(string));
        table.Columns.Add("LastName", typeof(string));

        _bindingSource.DataSource = table;
        dataGridView1.DataSource = _bindingSource;

    }

    private void ShowChildFormButton_Click(object sender, EventArgs e)
    {
        var childForm = new ChildForm();
        childForm.AddPerson += OnAddPerson;
        childForm.ShowDialog();
        childForm.Dispose();
    }

    private void OnAddPerson(string firstname, string lastname)
    {
        ((DataTable)_bindingSource.DataSource)
            .Rows
            .Add(firstname, lastname);
    }
}

Child form, pass values to MainForm if both TextBox controls are not empty.

public partial class ChildForm : Form
{
    public delegate void OnAdd(string firstName, string lastName);
    public event OnAdd AddPerson;
    public ChildForm()
    {
        InitializeComponent();
    }

    private void PostButton_Click(object sender, EventArgs e)
    {
        if (
            !string.IsNullOrWhiteSpace(FirstNameTextBox.Text) && 
            !string.IsNullOrWhiteSpace(LastNameTextBox.Text))
        {
            AddPerson?.Invoke(FirstNameTextBox.Text, LastNameTextBox.Text);
        }
    }
}
Karen Payne
  • 4,341
  • 2
  • 14
  • 31