1

I want to add a row from multiple textbox, a datetimepicker and a combobox (form2) to datagridview (form1). If i try to add the row it won't add up. What i really want is to have an integrated autosave/autoload and i want to create a blank datagridview with only the columns so the user can manually add the rows of data. i will explain a little bit more about what i'm building here; Form1: will have a datagridview (to store some user added data), 3 textboxes (textbox1 = total amount of column[5], textbox2 = total amount of column[5] where column[6] is "Nee", textbox3 = textbox1 - textbox2) and a button to open form2; Form2: Will have 1 datetimepicker, 5 textboxes, 1 combobox and a button wich will add the datetimepicker, 5 textboxes and combobox as a new row in the datagridview. i also want the data to be autosaved and autoload when closing and opening the progam. this is what i got so far:

program:

using System;
using System.Windows.Forms;

namespace Javell_Administratie_Software
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run (new Overzicht());
        }
    }
} 

form1:

using System;
using System.Data;
using System.Windows.Forms;

namespace Javell_Administratie_Software
{
    public partial class Overzicht : Form
    {
        public Overzicht()
        {
            InitializeComponent();
            dt = table;
            Overzicht1.DataSource = dt;
        }

        public DataTable dt = new DataTable();

        public DataSet ds = new DataSet();

        public DataTable table
        {
            set
            {                
                foreach (DataGridViewColumn col in Overzicht1.Columns)
                {
                    dt.Columns.Add(col.Name);
                    col.DataPropertyName = col.Name;
                }
                ds.Tables.Add(dt);                
            }
            get
            {
                return dt;
            }
        }

        public void AddDataTableRow()
        {
            Toevoegen tv = new Toevoegen();
            object row = new object[]
            {
                tv.dateTimePicker1.Value, tv.textBox1.Text,
                tv.textBox2.Text, tv.textBox3.Text, tv.textBox4.Text,    
                tv.textBox5.Text, tv.comboBox1.Text
            };
            dt.Rows.Add(row);        
            Overzicht1.DataSource = dt;
            Overzicht1.Update();
            tv.Close();
        }

        public void Toevoegen1_Click(object sender, EventArgs e)
        {
            Toevoegen tv = new Toevoegen();
            tv.Show();
        }
    }
}

form2:

using System;
using System.Windows.Forms;

namespace Javell_Administratie_Software
{

    public partial class Toevoegen : Form
    {

        public Toevoegen()
        {
            InitializeComponent();
        }       

        public void Toevoegen2_Click(object sender, EventArgs e)
        {
            Overzicht oz = new Overzicht();
            oz.AddDataTableRow();            
            oz.Overzicht1.DataSource = oz.dt;
            oz.Overzicht1.Update();
            this.Close();
        }
    }
}
Javell
  • 33
  • 3

2 Answers2

0

You must add public parameter into form2

 using System;
    using System.Windows.Forms;

    namespace Javell_Administratie_Software
    {

        public partial class Toevoegen : Form
        {

            public Toevoegen()
            {
                InitializeComponent();
            }       
            public object Row{get;set;}
            public void Toevoegen2_Click(object sender, EventArgs e)
            {
                Row=  new object[]{
                tv.dateTimePicker1.Value, tv.textBox1.Text,
                tv.textBox2.Text, tv.textBox3.Text, tv.textBox4.Text,    
                tv.textBox5.Text, tv.comboBox1.Text
                };
            this.Close();
            }
        }
    }

and edit Form1

     public void AddDataTableRow(Object row)
            {

                dt.Rows.Add(row);        

            }

            public void Toevoegen1_Click(object sender, EventArgs e)
            {
                Toevoegen tv = new Toevoegen();
                tv.ShowDialog();
                AddDataTableRow(tv.Row);
            }
KreminT
  • 169
  • 2
  • 12
0

Okay this is how I would tackle your problem. However it might required substantial code rework.

  1. For the autosave/autoload you need to have create the Window_Closing event to call a serialize command on your objects. Then at the beginning of your main window when it initializes it will need to attempt to deserialize the file you serialized to. Scroll down to the answers section to see several ways to implement the method calls on how to serialize and deserialize. How to save/restore serializable object to/from file?

  2. This goes in tandem with 1, you really want to make create a class that represents the data that is going into your datatable that will have properties that correspond to the textboxes and datatimepicker, etc. Form2 will have a property that returns an object of said class by initialize it with the data from those UI controls. So after you call form2 from form1, you can do form2.GetMyObject (whatever you name the property that returns the object) and add it to the collection with whateveryourcollectionis.Add(form.GetMyObject). You might be asking now why go through this? Well the reason is you can then create a list or some other kind of collection where you add your class objects and BIND that collection to the itemsSource of the datagridview. It is good practice to separate the UI logic from the data/business logic. This will be important for the last item. This will allow for easier code maintenance and when you need to make changes it will require less code; the down side is implementing it at the beginning might require initially more work.

  3. You are finally going to need an eventhandler that once an item is added to your collection/datatable that it will need to update the total and the difference and computing this will be easier when dealing with objects than parsing the data directly from the datagridview.

Community
  • 1
  • 1
peterpep
  • 314
  • 3
  • 11
  • thanx this gives me a lot to read, learn & try :P i will give you the solved now because this is probably the wright answer and i let you know when i got it to work! keep you posted. – Javell Dec 12 '16 at 15:54
  • @Javell just a note on why adding data directly into the datagridview may not be the best way and why developing a class might be the way to go. If you will need to update, edit or use the data, a class would be ideal. For example, let's say you want to grab the DateTimePicker information, the way you are accessing it now would be by hardcoding what column and row that information is in But let's say you want to include another column in your datagridview this is going to cause you need now change your logic as the column could shift. The way you are doing it now is possible but will be harder – peterpep Dec 12 '16 at 16:07