0

I am quite new to programming and I am usually learning from courses on YouTube or directly tutorials from Youtube. My problem is I want to load file from CSV file and then update the loaded file into DataGridView. But to do so I was following a tutorial but I dunno what table to use.

private void btnOtvor_Click(object sender, EventArgs e)
        {
            try
            {
                using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true, Multiselect = false })
                {
                    if (ofd.ShowDialog() == DialogResult.OK)
                    dataGridViewZamestnanci.DataSource = Readcsv(ofd.FileName);                                     
                    labelErrMsg.Text = "Subor otvoreny spravne";
                    labelErrMsg.ForeColor = System.Drawing.Color.Green;
                    timer1.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

But when I am creating the ADD button:

private void btnPridat_Click(object sender, EventArgs e)
        {
            dataGridViewZamestnanci.Rows.Add(textBoxMeno.Text, textBoxTel.Text, textBoxProfesia.Text, textBoxTpp.Text);
            dataGridViewZamestnanci.DataSource = dataGridViewZamestnanci;

it doesnt see the DatagridviewZamestnanci, there should be datatable but since I loaded it from CSV file directly I am confused and honestly lost...

Can somebody help me, please ?

Dairy
  • 9
  • 4
  • One thing you should be aware of… _”You cannot programmatically add rows directly into the grid when the grid is data bound”_ … so the code… `dataGridViewZamestnanci.Rows.Add(textBoxMeno.Text, textBoxTel.Text, textBoxProfesia.Text, textBoxTpp.Text);` … will not work. – JohnG Jul 01 '21 at 21:11
  • Is what you want to do is add the “new” row to the grids data source not the grid itself. If `Readcsv(ofd.FileName)` returns a `DataTable`, then, is what you could do is cast the grids data source to a `DataTable` like… `DataTable dt = (DataTable)dataGridViewZamestnanci.DataSource;` … then add the new row to “that” `DataTable`, `dt`. Something like… `dt.Rows.Add(textBoxMeno.Text, textBoxTel.Text, textBoxProfesia.Text, textBoxTpp.Text);` – JohnG Jul 01 '21 at 21:11

1 Answers1

0

As I commented, You will get an error stating that... "you cannot programmatically add rows to a grid that is data bound." ... If you attempt to execute the line of code…

dataGridViewZamestnanci.Rows.Add(textBoxMeno.Text, textBoxTel.Text, textBoxProfesia.Text, textBoxTpp.Text);

you will get this error.

However, since you claim the grids data source is a DataTable, then, you could “cast” the grids DataSource to a DataTable, then add the new row to “that” DataTable instead of directly adding the row to the grid. This change may look something like…

DataTable dt = (DataTable)dataGridViewZamestnanci.DataSource;
dt.Rows.Add(textBoxMeno.Text, textBoxTel.Text, textBoxProfesia.Text, textBoxTpp.Text);

If the code is going to add/update/delete rows in the grid, then, it may be easier to make the grids DataSource a “global” variable. Then, you can avoid “casting” the grids DataSource to a DataTable each time you want to add a row. To do this, you would define a global DataTable variable called something like GridDT

DataTable GridDT;

Then, in the current code, change…

dataGridViewZamestnanci.DataSource = Readcsv(ofd.FileName);

To…

GridDT = Readcsv(ofd.FileName);
dataGridViewZamestnanci.DataSource = GridDT;

then the second code snippet above could be simplified to…

GridTDT.Rows.Add(textBoxMeno.Text, textBoxTel.Text, textBoxProfesia.Text, textBoxTpp.Text);

I hope this makes sense.

JohnG
  • 9,259
  • 2
  • 20
  • 29
  • Thanks ! I will try your suggestions, I hope I will be able to understand. :) So excited to try it on this weekend. – Dairy Jul 02 '21 at 22:05
  • Gosh, sir. It worked like a charm !!! Thank you SO much. :) Cheers and I wish you nice weekend ! :) – Dairy Jul 02 '21 at 23:28