0

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?

Smarc
  • 55
  • 1
  • 8

1 Answers1

1

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;
    }
Z.R.T.
  • 1,543
  • 12
  • 15
  • Thanks for your answer, but if I remove line 18, how do I pass the actual BindingSource? – Smarc Sep 24 '17 at 17:22
  • Okay, now I am a little confused. I get your new function and it makes sense to me. But now I get a System.NullReferenceException: 'Object reference not set to an instance of an object.' bs was null. in NewEntry.cs – Smarc Sep 24 '17 at 17:27
  • use mainForm.Bs as parameter in method. show me your addNewEntry and how you call it – Z.R.T. Sep 24 '17 at 17:29
  • I updated my Git: [link]https://github.com/Smarcy/PassManager Callin in frmAddNewEntry.cs, function in NewEntry.cs – Smarc Sep 24 '17 at 17:35
  • public frmMain() { BS = new BindingSource(); – Z.R.T. Sep 24 '17 at 17:40
  • Fixed that one, now "SavePasswords.cs" gives me an "System.InvalidOperationException: 'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.'" in line 61 :s – Smarc Sep 24 '17 at 17:47
  • that is different story amigo.. recommend you look through this links: https://stackoverflow.com/questions/8708057/rows-cannot-be-programmatically-added-to-the-datagridviews-row-collection-when and https://stackoverflow.com/questions/28351471/error-rows-cannot-be-programatically-added-to-datagridviews-row-collection-whe . work with dataCollection used for DataSource of your DataGridView instead of direct call DataGridView.rows – Z.R.T. Sep 24 '17 at 18:08
  • last edit... my advice: add or remove a row do with data collection instead of instance of BidingSource or property dataGridView.DataSource – Z.R.T. Sep 24 '17 at 18:22