Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

c# - Updating a datagridview from a different form

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...