简体   繁体   中英

C# & SQL server with data grid: Adding to A seperate database from data grid view

So, I know that you can populate information to a data grid from a SQL server. But is there a reverse process but to a different table to do so?

For instance having a master list and using other tables.

Example: Master Table: (Displayed on datagrid1 )

| OtherTableName|  TestSubj  | TestCriteria  |
----------------------------------------------
|    TableName  | ValueSubj  | ValueCriteria | 
----------------------------------------------
|    TableName  | ValueSubj  | ValueCriteria |
---------------------------------------------- 

Other Table

| TestSubj  | TestCriteria  | 
-----------------------------
| ValueSubj | ValueCriteria |
-----------------------------
| ValueSubj | ValueCriteria | 
-----------------------------

I want to pull the matching columns from a single row from Master Table (DataGridView1) to Other Table (SQL Database).

So essentially, you would click a row in DataGridView1 then click a button "Add Row to Other Table". Which would add your insert command to the SQL database, in this case it would Exclude the "OtherTableName" column and only insert the "TestSubj" and "TestCriteria" into "OtherTable" Table..

Is this at all possible? I've tried searching for some documentation on this, but I can't seem to find anything.

I'm sure there's a much simpler method in doing this.However, You can just use the strings from the selected rows cell Value. The user must select entire row You can change these settings in Visual Studio under datagridview properties.

    private void button1_Click_1(object sender, EventArgs e)
    {
        string iOne = dgMasterGridView.SelectedRows[0].Cells[1].Value + string.Empty;
        string iTwo = dgMasterGridView.SelectedRows[0].Cells[2].Value + string.Empty;
        string iThree = dgMasterGridView.SelectedRows[0].Cells[3].Value + string.Empty;
        string iFour = dgMasterGridView.SelectedRows[0].Cells[4].Value + string.Empty;
        string iFive = dgMasterGridView.SelectedRows[0].Cells[5].Value + string.Empty;
        string iSix = dgMasterGridView.SelectedRows[0].Cells[6].Value + string.Empty;
        string iSeven = dgMasterGridView.SelectedRows[0].Cells[7].Value + string.Empty;
        string iEight = dgMasterGridView.SelectedRows[0].Cells[8].Value + string.Empty;
        string iNine = dgMasterGridView.SelectedRows[0].Cells[9].Value + string.Empty;
        string iTen = dgMasterGridView.SelectedRows[0].Cells[10].Value + string.Empty;
        string iEleven = dgMasterGridView.SelectedRows[0].Cells[11].Value + string.Empty;
        string iTwelve = dgMasterGridView.SelectedRows[0].Cells[12].Value + string.Empty;

        try
        {
            // Connection to DB
            SqlConnection con = new SqlConnection();
            con.ConnectionString = (@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Database;Integrated Security=True");
            //Insert Query
            string insertquery = "INSERT INTO dbo.[myTable] ([Item1], [Item2], [Item3], [Item4], [Item5], [Item6], [Item7], [Item8], [Item9], [Item10], [Item11], [Item12]) VALUES(@Item1,@Item2,@Item3,@Item4,@Item5,@Item6,@Item7,@Item8,@Item9,@Item10,@Item11,@Item12)";

            SqlCommand cmd = new SqlCommand(insertquery, con);

            //open connection
            con.Open();

            //Parameters
            cmd.Parameters.AddWithValue("@Item1", iOne);
            cmd.Parameters.AddWithValue("@Item2", iTwo);
            cmd.Parameters.AddWithValue("@Item3", iThree);
            cmd.Parameters.AddWithValue("@Item4", iFour);
            cmd.Parameters.AddWithValue("@Item5", iFive);
            cmd.Parameters.AddWithValue("@Item6", iSix);
            cmd.Parameters.AddWithValue("@Item7", iSeven);
            cmd.Parameters.AddWithValue("@Item8", iEight);
            cmd.Parameters.AddWithValue("@Item9", iNine);
            cmd.Parameters.AddWithValue("@Item10", iTen);
            cmd.Parameters.AddWithValue("@Item11", iEleven);
            cmd.Parameters.AddWithValue("@Item12", iTwelve);

            //execute
            cmd.ExecuteNonQuery();

            //close connection
            con.Close();

            MessageBox.Show("Information has been submitted");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

This also skips the 1st column. However, If you wanted to pull the information from the 1st column, just add:

string iZero = dgMasterGridView.SelectedRows[0].Cells[0].Value + string.Empty;

&&

cmd.Parameters.AddWithValue("@Item0", iZero);

Don't forget to add it to your SQLQuery string also.

You can also Store the information in a Var for later use inside or outside of your scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM