You must Sign In to post a response.
  • Category: Windows

    Issue with Auto Refresh in Gridview

    HI,
    I have developed gridview for my windows application.To get data from database.
    I am using UPDATE logic to update new record but i need an logic for Autorefresh in gridview directly when i update record manually the value should be updated without refreshing page manually(Auto refresh is According to requirement in my application).
  • #684236
    hi,

    for this you can follow below steps:

    first you update the data in database after editing in the gridview. or something

    second now after updating the database again query the database to get the new data from database and bind to it.
    this way you can do it. but this will be hitting many times to database whenever you update the database (if you database is updating many time then it gives you the performance problem.

    Rayala HariKishore

    try..try..try...you achieved it.
    http://rayalaharikishore.wordpress.com/

  • #684292
    Hi,

    Write one common method like below and use that method to refresh data. In below code i have refresh datagridview when leave from one row call that method when you required


    using System.Data;
    using System.Data.SqlClient;

    namespace DataGridViewRefresh
    {
    public partial class Form1 : Form
    {
    SqlConnection sqlcon = new SqlConnection(@"Server=RAVI-PC\SQLEXPRESS;database=test1;uid=ravindran;pwd=srirangam;");
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    LoadGrid();
    }

    void LoadGrid()
    {
    sqlcon.Open();
    sqlcmd = new SqlCommand("select * from emp", sqlcon);
    da = new SqlDataAdapter(sqlcmd);
    da.Fill(dt);
    bind = true;
    dataGridView1.DataSource = dt;
    sqlcon.Close();
    }
    private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
    LoadGrid();
    }
    }
    }

    Regards
    N.Ravindran
    Your Hard work never fails

  • #684350
    You can try this code



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

    namespace WindowsFormsApplication2
    {
    public partial class Form2 : Form
    {
    int itickcount = 0;

    public Form2()
    {
    InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
    Get_Data();

    }

    private void Get_Data()
    {
    SqlConnection Sqlcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=yourdatabase;User ID=sa;Password=yourdbpassword");
    SqlCommand Sqlcmd = new SqlCommand("select * from tbl_Customers",Sqlcon);
    DataSet ds = new DataSet();
    SqlDataAdapter SqlAda = new SqlDataAdapter(Sqlcmd);
    SqlAda.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0];
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    /* Set Timer interval =1000 ms (1sec)*/
    Get_Data();

    /* Testing */
    itickcount++;
    this.Text = " Auto Refreshed" + itickcount.ToString();
    }
    }
    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.