how to refresh form1 datagridview automatically when data updated on form2
  • Actually i am extending this article and getting all data from this link http://www.codingresolved.com/discussion/2036/login-in-and-logout-with-current-login-user-list/p1
    In this article i am going to show how can we update data in form1 from database when database get effect from another form.

    I am getting timer and i static variable on form1 this variable can access from form2 on save and update when this variable will true so data will auto fill in form1 datagridview from database.

    Source Code is Also attach you can download it.



    image

    Form1 Code



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace WindowsFormsApplication4
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    Timer timer1 = new Timer();
    public static bool IsDataChanged = true;

    private void Form1_Load(object sender, EventArgs e)
    {
    timer1.Tick += new EventHandler(timer1_Tick); // add event to timer who perform on every tick

    timer1.Interval = (1000); // Timer will tick on every second
    timer1.Enabled = true; // Enable the timer
    timer1.Start();

    SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter("select * from tblUser ", con);
    sda.Fill(dt);
    }

    void timer1_Tick(object sender, EventArgs e)
    {
    if (IsDataChanged == true)
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter("select UserName 'Online Users' from tblUser where LoginStatus=1", con);
    sda.Fill(dt);
    dgUser.DataSource = dt;
    IsDataChanged = false;
    }
    }

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


    }
    }
    Form2 Code



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;

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

    private void Form2_Load(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter("select * from tblUser ", con);
    sda.Fill(dt);
    cmbUser.DataSource = dt;
    cmbUser.DisplayMember = "UserName";
    cmbUser.ValueMember = "UserId";
    }

    private void btnLogIn_Click(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    con.Open();
    cmd.CommandText = "Update tblUser set LoginStatus=1 where UserId=" + cmbUser.SelectedValue.ToString();
    cmd.ExecuteNonQuery();
    con.Close();
    Form1.IsDataChanged = true;
    }

    private void btnLogOut_Click(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    con.Open();
    cmd.CommandText = "Update tblUser set LoginStatus=0 where UserId=" + cmbUser.SelectedValue.ToString();
    cmd.ExecuteNonQuery();
    con.Close();
    Form1.IsDataChanged = true;
    }
    }
    }
    DataGridViewRefresh.jpg
    627 x 356 - 38K
    WindowsFormsApplication4.zip
    48K
  • 1 Comment sorted by
  • thanks @ramesh for sharing and enhance my code for others :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Top Posters Weekly

Optimum Creative