Goal:
Having two buttons that should be enable to add or delete data from datagridview. The changes should be make in real time so you should be see the new result.
Problem:
Having problem to display the new result after I have used button add functionality because the result won't display in real time. In order to view the result I have to close and reopen the application in order to view the new result.
Please remember that I don't use a database.
I'm using class table and then I connect the table to the datagridview's datasource.
// JT
DataTable table = new DataTable();
table.Columns.Add("a");
table.Columns.Add("b");
foreach (var a in myManagerProduct.GetAllProductList())
{
DataRow row;
row = table.NewRow();
row["a"] = a._articleNumber;
row["b"] = a._name;
dgridStock.Rows.Add(row);
table.Rows.Add(row);
}
dgridStock.DataSource = table;
Loading

Vijay YadavRank #1,637 · 943 reputationPosted Mar 16, 2011, 1:28 AM EDT
On delete button, after deletig record again call the function that populate the datagridview with data and your query will get solved.:)
J T13 reputationPosted Mar 16, 2011, 2:42 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 16, 2011, 3:23 AM EDT
J T13 reputationPosted Mar 16, 2011, 3:30 AM EDT
foreach (DataGridViewRow a in dgridStock.SelectedRows)
{
string articleNumber = a.Cells[0].Value.ToString();
string name = a.Cells[1].Value.ToString();
myDeleteProduct.DisplayingADeleteMessagePopup(articleNumber, name);
dgridStock.Rows.Remove(a);
}
Sourcecode för button:
myNewProduct = new NewProduct();
myNewProduct.ShowDialog();
AFter showdalog you go back to method UpDateGUI(). THe sourcode to this method is located in the first message in this discusstion.
VulpesRank #55 · 29.2K reputationPosted Mar 16, 2011, 4:13 AM EDT
dgridStock.Refresh(); // if necessary
J T13 reputationPosted Mar 16, 2011, 8:21 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 16, 2011, 11:19 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Mar 16, 2011, 12:26 PM EDT
J T13 reputationPosted Mar 19, 2011, 8:03 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 19, 2011, 11:01 AM EDT
J T13 reputationPosted Mar 21, 2011, 10:50 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Mar 21, 2011, 5:54 PM EDT
The following will quickly show you an alternative. If you create a new (forms) project, then this can be done in a few minutes literally.
After creating the project, in Solution Explorer, add a DataSet (Right-click on project then Add | New Item | Visual C# Items | Data | DataSet).
In the toolbox, drag a DataTable to the DataSet. Right-click on the DataTable and add columns.
Go to the form designer. From the Data Sources window, drag the datatable to the form. By default, a DataGridView will be created for the DataTable.
Build and test the program. You can add, update and delete records right in the DataGridView. The data won't be saved when you close the form but we have not provided a place to save it.
End of demonstration.
See my Database Table Update in a DataGridView without Writing Code for a little help for saving the data. If you simply must have buttons for adding and deleting records, then that could be done.