Link to home
Start Free TrialLog in
Avatar of pat_cunningham
pat_cunningham

asked on

DataGridView Update

What is a good way to update a datagridview regularly (every 15 seconds) and not interfere with someone that might be scrolling through the data.  The table is not editable, it is for viewing only.  I tried using the scroll and mouse events but nothing I have done so far seems very robust.
Avatar of Member_2_786565
Member_2_786565
Flag of United States of America image

Create a Form with a DataGridView, DataSet, and Timer.  Scrolling appears to function well.

    public partial class Form1 : Form
    {
        DataTable dt = new DataTable("test1");
        int count = 0;

        public Form1() {
            InitializeComponent();

            string stringVar = "";

            dt.Columns.Add("col1", stringVar.GetType());
            dataSet1.Tables.Add(dt);

            dataGridView1.DataSource = dataSet1;
            dataGridView1.DataMember = "test1";
        }

        private void timer1_Tick(object sender, EventArgs e) {
            DataRow row1 = dt.NewRow();
            row1["col1"] = "test" + (count++).ToString();
            dt.Rows.Add(row1);
        }
    }
Note: the key to the last post is the data binding.. using the .DataSource and .DataMember properties of hte DataGridView.
Avatar of pat_cunningham
pat_cunningham

ASKER

I am using a timer and setting the datasource every 15 seconds, the problem is someone is scrolling through the data and I set the datasource it moves the scroll to the top.
Okay - sounds like your DataGridView is bound to an object that is getting regenerated each time.  Suggestion: create a static DataTable and bind your DataGridView to that.  Then every 15 sec, just update the table - don't rebind.  It make take some work to "sync" the new static table to your previous data source.  Do you have any more details?
Sounds like a good idea but I my table will update, can you tell what I am doing wrong?

    Private dtLoadBoard As New DataTable

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Sql As String = "SELECT * "
        Sql &= "FROM vw_CG_LB_LoadBoardNew "

        Try
            'Define command
            Using da As New SqlDataAdapter(Sql, My.Settings.MaterialsConnectionString)
                'Fill dataset
                da.Fill(dtLoadBoard)
            End Using
        Catch ex As Exception
            db.SendEmail(My.Settings.AdminEmail, ex.ToString)
        End Try

        With dgvLoadBoard
            .DataSource = dtLoadBoard
        End With
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        db = Nothing

    End Sub

    Private Sub tmrMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMain.Tick

        Dim Sql As String = "SELECT * "
        Sql &= "FROM vw_CG_LB_LoadBoardNew "

        Try
            'Define command
            Using da As New SqlDataAdapter(Sql, My.Settings.MaterialsConnectionString)
                'Fill dataset
                da.Fill(dtLoadBoard)
            End Using
        Catch ex As Exception
            db.SendEmail(My.Settings.AdminEmail, ex.ToString)
        End Try

        dgvLoadBoard.Update()

    End Sub
Sorry that first sentence was supposed to say -
Sounds like a good idea but I cannot get my table to update. Can you tell what I am doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_786565
Member_2_786565
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial