I have a solution that has 2 forms in it. One of them has a datagridview that I have written a method for that updates the datagridview with data as follows:
//Load Data Into Supplier Table
SQLiteConnection dbConnectionString;
dbConnectionString = new SQLiteConnection("Data Source=F:\Purchasing - Do not delete\Purchasing Procedure\PurchasingDB.sqlite;Version=3;");
dbConnectionString.Open();
string sqlSuppliers = "SELECT * FROM Supplier";
var sqlDataAdapt = new SQLiteDataAdapter(sqlSuppliers, dbConnectionString);
using (DataTable dt = new DataTable())
{
sqlDataAdapt.Fill(dt);
SupplierDGrid.DataSource = dt;
}
dbConnectionString.Close();
I have another form that acts as in input form and is adding records to the same table that is mentioned in the query above so I want it to update the datagrid view on the first form when the button is pressed to run the insert query.
private void button1_Click(object sender, EventArgs e)
{
//Create New Supplier Button
SQLiteConnection dbConnectionString;
dbConnectionString = new SQLiteConnection("Data Source=F:\Purchasing - Do not delete\Purchasing Procedure\PurchasingDB.sqlite;Version=3;");
dbConnectionString.Open();
DateTime spCreateDateVar = DateTime.Today;
string supplierTblSQL = "INSERT INTO Supplier (spSupplierID,spSupplierName, spCreatedDate, spActive) VALUES (@spSupplierID,@spSupplierName, @spCreateDateVar,1) ";
SQLiteCommand command = new SQLiteCommand(supplierTblSQL, dbConnectionString);
//Add the perameters to the query
using (command)
{
command.Parameters.AddWithValue("@spCreateDateVar", spCreateDateVar);
command.Parameters.AddWithValue("@spSupplierName", txtSuppName.Text);
command.Parameters.AddWithValue("@spSupplierID", maxSuppID());
command.ExecuteNonQuery();
dbConnectionString.Close();
}
}
As the method and the datagrid view are in a separate form to the insert query i am not sure how to go about doing that. Can anyone advise me?
question from:
https://stackoverflow.com/questions/65889218/updating-a-datagridview-from-a-different-form 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…