Search code examples
c#winformsdatagridview

add a row from multiple textbox, a datetimepicker and a combobox to datagridview other form c#


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 which 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 program. 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();
        }
    }
}

Solution

  • 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.