How I can refresh my dataGridView from another form?

Answered How I can refresh my dataGridView from another form?

  • Tuesday, April 03, 2012 8:47 PM
     
     
    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 CoolDadTxMVP Friday, April 06, 2012 5:12 PM Winforms related (From:Visual C# General)
    •  

All Replies

  • Tuesday, April 03, 2012 10:39 PM
     
     
    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.
  • Wednesday, April 04, 2012 7:18 AM
     
      Has Code

    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 04, 2012 8:01 AM
     
     Answered Has Code

    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)

  • Friday, April 06, 2012 4:57 AM
     
     Answered Has Code

    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 06, 2012 5:49 AM
     
     Answered
  • Friday, April 06, 2012 11:11 AM
     
     Answered Has Code

    Hi

    First of all you must define a public static List in form1. When you open form2 it's better that you add your data to that List that you created in form1. and when you close it, you can refer to a method that its work is matching the list content with DataGridView content, you can bind its content to DataGridView.

    namespace WindowsFormsApplication1
    {
        public struct RegisterStruct
        {
            public int ID;
            public string Name ;
            public string LastName ;
            ....
        }
    
        public partial class Form1: Form
        {
            
            public static List<RegisterStruct> RegisterStructList = new List<RegisterStruct>();
    
    
     private void btnInsert_Click(object sender, EventArgs e)
            {
                Form2 frm2= new From2();
                frm2.ShowDialog();
    
                fillDgv();
    
            }
    
    public void fillDgv()
            {
                dataGridView1.Rows.Clear();
    
                foreach (RegisterStruct rs in RegisterStructList)
                {
                    dataGridView1.Rows.Add(rs.ID,rs.Name, rs.LastName);
                }
            }
          }   
    }
    
    
    
    
    
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2: Form
        {
     private void btnOk_Click(object sender, EventArgs e)
            {
                RegisterStruct rs = new RegisterStruct();
    
                rs.Code = text1.text;
                rs.Name= text2.text;
                rs.LastName = text3.text;
    
                Form1.RegisterStructList.Add(rs);
    
                this.Dispose();
            }
         }
    }


    danialtehrani


  • Friday, April 06, 2012 1:03 PM
     
     Answered Has Code
    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