Reputation: 2416
I want to bind the DataGridView to a Table in my Database. I've found several topics about this, but I can't get it to work. Perhaps I'm missing something.
I'm building a "monitor" in which I want to see any Database changes of a specific table. So, after loading the data into a DataGridView, any change in that specific table should be propagate to the DataGridView.
Look what I've done:
SqlDataAdapter daTab;
DataTable dTable;
private void button1_Click(object sender, EventArgs e)
{
string s;
s = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\tempDB\Database1.mdf;Integrated Security=True";
SqlConnection conn= new SqlConnection(s);
conn.Open();
daTab = new SqlDataAdapter("Select * From tab", conn);
dTable = new DataTable();
daTab.Fill(dTable);
BindingSource bSource = new BindingSource();
bSource.DataSource = dTable;
dgv1.DataSource = bSource;
}
But it only updates the DataViewGrid when I run
daTab.Fill(dTable);
So I may have two options from here: 1) get it to work right (this is why I'm needing your help) or 2) put a timer and run daTab.Fill(dTable);
at some time interval. I am particullary looking after option 1). So, I'd appreciate any help given.
Upvotes: 2
Views: 1690
Reputation: 1808
It sounds like you're using SQL Server. If so, have a look at the SqlDependency class. You can create a delegate for this class's OnChange event and be notified of changes, which you can then propagate to your GridView. Here is a good writeup on the topic with some examples: https://msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx
Upvotes: 3