See the image below to understand better.
http://img18.imageshack.us/img18/9086/inventoryadd.jpg

When I click add button in form2 it doesn't show any changes in the datagridview does anyone know what code I should use so the datagridview changes when I click add.

I am now using a update button and that shows the changes made in the datagridview.
Here is the code I use in update button:

private void updateButton_Click(object sender, EventArgs e)
        {
            this.partsTableTableAdapter.Fill(this.partsDataSet.partsTable);

            MessageBox.Show("Updated");

        }

Recommended Answers

All 2 Replies

One way is to use events and delegates.
Declare a delegate in your namespace

public delegate void AddCarEventHandler(Car aCar);

in the form1 declare a method AddCar

private void AddCar(Car aCar) //Car is a class or struct that has all the elements you need to add. You can use multiple parameters if you want

in the form2, declare an event

public event AddCarEventHandler OnCarAdd;

on the button add on click event, you raise this event

OnCarAdd(Car theNewCar);

now, back on your form1. After you create a new instance of form2, add the following line

frmAdd.OnCarAdd += new AddCarEventHandler(AddCar);

For more information about events, check msdn documentation.

Ionut

I tried to put the codes where you told me to do but it showed errors and I couldn't find the errors. Can somebody please help me where to put those codes.
Here is my code.

Form1:

namespace ProjektBildelar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Form2().Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'partsDataSet.partsTable' table. You can move, or remove it, as needed.
            this.partsTableTableAdapter.Fill(this.partsDataSet.partsTable);

        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            this.partsTableTableAdapter.Fill(this.partsDataSet.partsTable);

            MessageBox.Show("Updated");

        }

        private void makeBox_TextChanged(object sender, EventArgs e)
        {
            DataView Dv = new DataView();

            Dv.Table = partsDataSet.Tables["partsTable"];
            Dv.RowFilter = " Make LIKE '%" + makeBox.Text + "%' AND Model LIKE '%" + modelBox.Text + "%' AND Part LIKE '%" + partBox.Text + "%' AND Description LIKE '%" + descriptionBox.Text + "%' ";
            dataGridView1.DataSource = Dv;
            
        }

        private void modelBox_TextChanged(object sender, EventArgs e)
        {
            DataView Dv = new DataView();

            Dv.Table = partsDataSet.Tables["partsTable"];
            Dv.RowFilter = " Make LIKE '%" + makeBox.Text + "%' AND Model LIKE '%" + modelBox.Text + "%' AND Part LIKE '%" + partBox.Text + "%' AND Description LIKE '%" + descriptionBox.Text + "%' ";
            dataGridView1.DataSource = Dv;

        }

        private void partBox_TextChanged(object sender, EventArgs e)
        {
            DataView Dv = new DataView();

            Dv.Table = partsDataSet.Tables["partsTable"];
            Dv.RowFilter = " Make LIKE '%" + makeBox.Text + "%' AND Model LIKE '%" + modelBox.Text + "%' AND Part LIKE '%" + partBox.Text + "%' AND Description LIKE '%" + descriptionBox.Text + "%' ";
            dataGridView1.DataSource = Dv;
        }

        private void descriptionBox_TextChanged(object sender, EventArgs e)
        {
            DataView Dv = new DataView();

            Dv.Table = partsDataSet.Tables["partsTable"];
            Dv.RowFilter = " Make LIKE '%" + makeBox.Text + "%' AND Model LIKE '%" + modelBox.Text + "%' AND Part LIKE '%" + partBox.Text + "%' AND Description LIKE '%" + descriptionBox.Text + "%' ";
            dataGridView1.DataSource = Dv;
        }

        private void clearBt_Click(object sender, EventArgs e)
        {
            makeBox.Text = "";
            modelBox.Text = "";
            partBox.Text = "";
            descriptionBox.Text = "";
        }
    }
}

FORM2:

namespace ProjektBildelar
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dt = new DataClasses1DataContext();
            partsTable pt = new partsTable();

            pt.Make = makeBox.Text;
            pt.Model = modelBox.Text;
            pt.Year = int.Parse(yearBox.Text);
            pt.Part = partBox.Text;
            pt.Description = descriptionBox.Text;
            pt.Quantity = int.Parse(quantityBox.Text);
            pt.Cost = int.Parse(costBox.Text);
            pt.Price = int.Parse(priceBox.Text);


            dt.partsTables.InsertOnSubmit(pt);
            dt.SubmitChanges();


            MessageBox.Show("Part added");

            makeBox.Text = "";
            modelBox.Text = "";
            yearBox.Text = "";
            partBox.Text = "";
            descriptionBox.Text = "";
            quantityBox.Text = "";
            costBox.Text = "";
            priceBox.Text = "";
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            makeBox.Text = "";
            modelBox.Text = "";
            yearBox.Text = "";
            partBox.Text = "";
            descriptionBox.Text = "";
            quantityBox.Text = "";
            costBox.Text = "";
            priceBox.Text = "";
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.