Smarc
Smarc

Reputation: 55

Adding new entry to DataGridView via BindingSource

Good day,

I'm writing a PasswordManager at the moment and am stuck with adding new rows to my DataGridView.

You can see my code over here: PassMangaer

The Engine/NewEntry.cs has the code for creating a new entry and adding it to the BindingSource. After that, the PassManger/frmAddNewEntry.cs adds it to the DataGridView on the main Form and refreshed the DataGridView.

Actually it just replaced the current row with the new one and does not, as it is supposed to, add a new row.

What am I missing here?

Upvotes: 0

Views: 288

Answers (1)

Z.R.T.
Z.R.T.

Reputation: 1603

your problem in frmAddNewEntry, line 18 , when your create BindingSource Bs = new BindingSource(). btnAddEntry_Click works with empty Bs. My suggestion:

  1. PassManager. Remove line 18
  2. public void addNewEntry(BindingSource bs, int id, string hoster)

  3. private void btnAddEntry_Click(object sender, EventArgs e) { string hoster = textBox1.Text; ne.addNewEntry(mainForm.Bs, 1, hoster); mainForm.RefreshDGV(); this.Close(); }

dont recommend to use, but that one would be fast hot fix for your last comment:

        public void LoadData(DataGridView grid)
    {
        DataTable dataTable = new DataTable();
        foreach (DataGridViewColumn col in grid.Columns)
        {
            dataTable.Columns.Add(new DataColumn(col.Name));
        }
        string file = "mygrid.bin";
        using (BinaryReader bw = new BinaryReader(File.Open(file, FileMode.Open)))
        {
            int n = bw.ReadInt32();
            int m = bw.ReadInt32();
            for (int i = 0; i < m; ++i)
            {
                dataTable.Rows.Add();
                for (int j = 0; j < n; ++j)
                {
                    if (bw.ReadBoolean())
                    {
                        dataTable.Rows[i][j] = bw.ReadString();
                        dataTable.Rows[i][j] = Base64Decode(dataTable.Rows[i][j].ToString());
                    }
                    else bw.ReadBoolean();
                }
            }
        }
        grid.DataSource = dataTable;
    }

Upvotes: 1

Related Questions