Updating my table's datagridviewcomboboxcolumns directly
[Note : I've simplified my example for clarity]
Let's say that I have a Sqlite database with two tables: Items and Sectors:
Items:
id_items : INTEGER PRIMARY KEY
name_item : VARCHAR(...)
id_sector : INTEGER
Sectors:
id_sector : I开发者_如何转开发NTEGER PRIMARY KEY
name_sector : VARCHAR(...)
I currently have a datagridview that is bound to the Items table. It gets fed alright, and the table displays the sector as a datagridviewcomboboxcolumns.
Hence, in my Winforms CustomControl, I have all the data loading and binding that occurs in the load() method:
colSector.DataSource = m_dataContext.SectorTable;
colSector.DisplayMember = "name_sector";
colSector.ValueMember = "id_sector";
ItemsGrid.DataSource = new DataView(m_dataContext.ItemsTable);
The comboboxes of my dataview are well loaded with the data from the Sectors table.
I now would like a button on my form that would allow the creation of a new sector:
I created a txtbox (txtNewSector) and a button that triggers the creation:
private void btnAddNewSector_Click(object sender, EventArgs e)
{
// Add new sector to db
m_dataContext.AddNewSector(newSectorName);
// refresh dataview so that comboboxes are updated with the new entry
???
}
How can I perform that refresh?
I hope the edit made the question more clear, please advise....
best regards
Instead of
???
add
ItemsGrid.DataSource = new DataView(m_dataContext.ItemsTable);
You can use a timer
for that. It will accomplish the task after a certain time interval.
Have a look at the example Timer in C#.
You will find the idea of using it.
I am give some rough sketch
private DataTable LoadData()
{
DataTable dt = LoadDatabaseData();
return dt;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
myDataGrid.DataSource = LoadData();
myDataGrid.Databind();
or
your combo box's datasource what ever.
}
Note- you are using SQLite regarding which I don't have any idea apart from it is a database. I have only showed you how to call the function that will be the datasource for your grid and after every 1 sec(I mean what ever time interval you specify) the datawill be refreshed.
Hope this helps.
精彩评论