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.
Note: the key to the last post is the data binding.. using the .DataSource and .DataMember properties of hte DataGridView.
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?
ASKER
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.MaterialsConne ctionStrin g)
'Fill dataset
da.Fill(dtLoadBoard)
End Using
Catch ex As Exception
db.SendEmail(My.Settings.A dminEmail, 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.MaterialsConne ctionStrin g)
'Fill dataset
da.Fill(dtLoadBoard)
End Using
Catch ex As Exception
db.SendEmail(My.Settings.A dminEmail, ex.ToString)
End Try
dgvLoadBoard.Update()
End Sub
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.MaterialsConne
'Fill dataset
da.Fill(dtLoadBoard)
End Using
Catch ex As Exception
db.SendEmail(My.Settings.A
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.MaterialsConne
'Fill dataset
da.Fill(dtLoadBoard)
End Using
Catch ex As Exception
db.SendEmail(My.Settings.A
End Try
dgvLoadBoard.Update()
End Sub
ASKER
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?
Sounds like a good idea but I cannot get my table to update. Can you tell what I am doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
}
}