none
How I can refresh my dataGridView from another form? RRS feed

  • Question

  • Hi Guys first of all I have two Forms  first = Form1 and second= Form2   and my dataGridView is on Form1 and my Registration is on Form2 .... When I register something on Form2 then I close my Form2 and I want to see the updates on Form1 on dataGridView How can I do it ? When I close my program and reopen it I can see that It updates 
    • Moved by CoolDadTx Friday, April 6, 2012 5:12 PM Winforms related (From:Visual C# General)
    Tuesday, April 3, 2012 8:47 PM

Answers

  • Hi Zerhan,

    Welcome to the MSDN forum.

    You can use custom events in this situation, you can try the sample below.

    Form1:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public static SqlConnection getc()
            {
                string sqlstr = "Data Source=.;Initial Catalog=pp1;Integrated Security=True";
                SqlConnection conn = new SqlConnection(sqlstr);
                return conn;
            }
            private DataTable GetData()
            {
                DataTable dt = new DataTable();
                using (SqlConnection conn = getc())
                {
                    SqlCommand cmd = new SqlCommand("select * from dgv", conn);
                    conn.Open();
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    ad.Fill(ds);
                    dt = ds.Tables[0];
                    return dt;
                }
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                dataGridView1.DataSource = GetData();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 fm = new Form2();
                fm.RefreshDgv += new Form2.DoEvent(fm_RefreshDgv);
                fm.Show();
            }
    
            void fm_RefreshDgv(string a, string b, string c)
            {
                using (SqlConnection conn = getc())
                {
                    SqlCommand cmd = new SqlCommand("insert into dgv values(@a,@b,@c)", conn);                
                    cmd.Parameters.AddWithValue("@a", a);
                    cmd.Parameters.AddWithValue("@b", b);
                    cmd.Parameters.AddWithValue("@c", c);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    dataGridView1.DataSource = GetData();               
                }
                
            }
        }

    Form2:

        public partial class Form2 : Form
        {
            public delegate void DoEvent(string a, string b, string c);
            public event DoEvent RefreshDgv;
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.RefreshDgv(textBox1.Text, textBox2.Text, textBox3.Text);
                this.Close();
            }   
        }

    Have a nice day.

    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 6, 2012 4:57 AM
  • I would rather suggest a simpler approach.

    1. In the Form1 place a public method which refreshes the grid

        
    public void RefreshGrid()
    {
      // set datasource
      // make sure you test this code
    }

    2. Invoke Form2 by passing the Form1 reference to it (through a method / use static variables)

    3. From the Form 2 after data updation, invoke the form1.RefreshGrid();


    Resolving n Evolving in C# (http://jeanpaulva.com)

    Wednesday, April 4, 2012 8:01 AM
  • Deleted
    Friday, April 6, 2012 11:11 AM
  • Hi Guys first of all I have two Forms  first = Form1 and second= Form2   and my dataGridView is on Form1 and my Registration is on Form2 .... When I register something on Form2 then I close my Form2 and I want to see the updates on Form1 on dataGridView How can I do it ? When I close my program and reopen it I can see that It updates 

    The question is where from you get data that populates dgv in the 1st place!?

    If this is from database, you should do a new INSERT query statement, and to reload, you should use SELECT query to populate dgv ones again.

    If you only want to "add" new row to dgv, you can simple do it this way:

    //on form1:
    //when opening form2:
    Form2 f2 = new Form2(this); //pass a reference of form1 to form2
    f2.Show();
    
    //a new method to add new row:
    public void AddingNewRow(object[] data)
    {
         dataGridView1.Rows.Add();
         for(int i = 0; i < data.Lenght; i++)
              dataGridView1[i, dataGridView1.Rows.Count -1].Value = data[i].ToString();
         dataGridView1.Refresh();
    }
    
    //on form2:
    Form1 f1;
    public Form2()
    {
       InitializeComponent(); //form2 constructor
    }
    
    public Form2(Form1 _f1)
       : this()
    {
         this.f1 = _f1;
    }
    
    private void buttonRegister() //button event
    {
        //get all data from all textBoxes together:
        TextBox[] tbs = new TextBox[] {textBox1, textBox2 }; //add all textboxes in here
         object[] data = new object[tbs.Length];
         for(int i = 0; i < data.Length; i++)
              data[i] = tbs[i].Text;
    
         //when you get all the data from textboxes, we can send then to form1, to do an update:
        f1.AddingNewRow(data);
    }


    Mitja

    Friday, April 6, 2012 1:03 PM

All replies

  • There a lot of ways to do that, you can create one property to get the value of your grid datasource,and set, and then in the other form, get this value. Other ways is when you register something, you update on database, and the next form, always made a new select from database keeping like that your updated values. Is just a simple ways to do that.
    Tuesday, April 3, 2012 10:39 PM
  • I tried to use  those code but It didn't work for me  but I didn`t understand exactly  Norkk How can I update my values ? I am a begginer at SQL Server I am sorry about  that Could you write down the codes?

      this.itemCategoryBindingSource.EndEdit();
           
    this.itemsTableAdapter.Fill(myDatabaseDataSet.Items);
           
    this.dataGridView1.Refresh();

    Wednesday, April 4, 2012 7:18 AM
  • I would rather suggest a simpler approach.

    1. In the Form1 place a public method which refreshes the grid

        
    public void RefreshGrid()
    {
      // set datasource
      // make sure you test this code
    }

    2. Invoke Form2 by passing the Form1 reference to it (through a method / use static variables)

    3. From the Form 2 after data updation, invoke the form1.RefreshGrid();


    Resolving n Evolving in C# (http://jeanpaulva.com)

    Wednesday, April 4, 2012 8:01 AM
  • Hi Zerhan,

    Welcome to the MSDN forum.

    You can use custom events in this situation, you can try the sample below.

    Form1:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public static SqlConnection getc()
            {
                string sqlstr = "Data Source=.;Initial Catalog=pp1;Integrated Security=True";
                SqlConnection conn = new SqlConnection(sqlstr);
                return conn;
            }
            private DataTable GetData()
            {
                DataTable dt = new DataTable();
                using (SqlConnection conn = getc())
                {
                    SqlCommand cmd = new SqlCommand("select * from dgv", conn);
                    conn.Open();
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    ad.Fill(ds);
                    dt = ds.Tables[0];
                    return dt;
                }
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                dataGridView1.DataSource = GetData();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 fm = new Form2();
                fm.RefreshDgv += new Form2.DoEvent(fm_RefreshDgv);
                fm.Show();
            }
    
            void fm_RefreshDgv(string a, string b, string c)
            {
                using (SqlConnection conn = getc())
                {
                    SqlCommand cmd = new SqlCommand("insert into dgv values(@a,@b,@c)", conn);                
                    cmd.Parameters.AddWithValue("@a", a);
                    cmd.Parameters.AddWithValue("@b", b);
                    cmd.Parameters.AddWithValue("@c", c);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    dataGridView1.DataSource = GetData();               
                }
                
            }
        }

    Form2:

        public partial class Form2 : Form
        {
            public delegate void DoEvent(string a, string b, string c);
            public event DoEvent RefreshDgv;
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.RefreshDgv(textBox1.Text, textBox2.Text, textBox3.Text);
                this.Close();
            }   
        }

    Have a nice day.

    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 6, 2012 4:57 AM
  • Deleted
    Friday, April 6, 2012 11:11 AM
  • Hi Guys first of all I have two Forms  first = Form1 and second= Form2   and my dataGridView is on Form1 and my Registration is on Form2 .... When I register something on Form2 then I close my Form2 and I want to see the updates on Form1 on dataGridView How can I do it ? When I close my program and reopen it I can see that It updates 

    The question is where from you get data that populates dgv in the 1st place!?

    If this is from database, you should do a new INSERT query statement, and to reload, you should use SELECT query to populate dgv ones again.

    If you only want to "add" new row to dgv, you can simple do it this way:

    //on form1:
    //when opening form2:
    Form2 f2 = new Form2(this); //pass a reference of form1 to form2
    f2.Show();
    
    //a new method to add new row:
    public void AddingNewRow(object[] data)
    {
         dataGridView1.Rows.Add();
         for(int i = 0; i < data.Lenght; i++)
              dataGridView1[i, dataGridView1.Rows.Count -1].Value = data[i].ToString();
         dataGridView1.Refresh();
    }
    
    //on form2:
    Form1 f1;
    public Form2()
    {
       InitializeComponent(); //form2 constructor
    }
    
    public Form2(Form1 _f1)
       : this()
    {
         this.f1 = _f1;
    }
    
    private void buttonRegister() //button event
    {
        //get all data from all textBoxes together:
        TextBox[] tbs = new TextBox[] {textBox1, textBox2 }; //add all textboxes in here
         object[] data = new object[tbs.Length];
         for(int i = 0; i < data.Length; i++)
              data[i] = tbs[i].Text;
    
         //when you get all the data from textboxes, we can send then to form1, to do an update:
        f1.AddingNewRow(data);
    }


    Mitja

    Friday, April 6, 2012 1:03 PM
  • Thanks a lot Good Solution...
    Sunday, June 14, 2015 7:03 PM
  • Can you please show me this with proper example. I couldnt perform this.
    Saturday, June 23, 2018 1:41 PM
  • Thank you this helpfull for me
    Thursday, September 19, 2019 10:10 AM