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
Sam HobbsPosted Mar 21, 2011, 9:54 PM
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.
J TPosted Mar 21, 2011, 2:50 PM
VulpesPosted Mar 19, 2011, 3:01 PM
J TPosted Mar 19, 2011, 12:03 PM
Suthish NairPosted Mar 16, 2011, 4:26 PM
VulpesPosted Mar 16, 2011, 3:19 PM
J TPosted Mar 16, 2011, 12:21 PM
VulpesPosted Mar 16, 2011, 8:13 AM
dgridStock.Refresh(); // if necessary
J TPosted Mar 16, 2011, 7:30 AM
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.
VulpesPosted Mar 16, 2011, 7:23 AM
J TPosted Mar 16, 2011, 6:42 AM
Vijay YadavPosted Mar 16, 2011, 5:28 AM
On delete button, after deletig record again call the function that populate the datagridview with data and your query will get solved.:)