简体   繁体   中英

Inserting a row in database from DataGridview and Textfield

My problem is, I want to save data in the database from DataGridview and a Textbox. Here's my sample code:

            connect.ConnectionString = coo;
            connect.Open();
            string str = string.Concat("insert into Sales values('", (1st column item in the datagridview), "','", textBox2.Text, "','", textBox3.Text, "','", textBox4.Text, "');");
            command = new OleDbCommand(str, connect);
            command.ExecuteNonQuery();
            command.Connection = connect;
            connect.Close();

Please provide guidance.

Inserting datGridView all rows to database:

We know to inserting values from textboxes and other data entry controls to database but the values enter from dataGridView to database is little different than those ways because here we have to insert all the rows which contains in dataGridView to database. Click here for inserting values from textboxes to the datagridview control.So for the we have to run a loop to to collect all the data from the all the rows from the database.

Event : The event to raise this code is saveButton_Click

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\naresh\My stuff\Thal tre tsks trim\Thalassemia\Data\thalsemia.accdb");
con.Open();

for (int i = 0; i < dataGridView1.Rows.Count; i++)

{

   OleDbCommand cmd= new OleDbCommand("INSERT INTO table1(name,number,salory,) VALUES
 ('"+dataGridView1.Rows[i].Cells["Column1"].Value+"','"+dataGridView1.Rows[i].Cells["Column2"].Value+"',
'"+dataGridView1.Rows[i].Cells["Column3"].Value+" ' ",con);

   cmd.ExecuteNonQuery();

}

con.Close();

I hope this code will help you.

to get the selected cell value you can use:

string Selection;
if (dataGridView1.SelectedRows.Count != 0)
{
    DataGridViewRow row = this.dataGridView1.SelectedRows[0];
    Selection=row.Cells[0].Value.ToString();
}

to add them in your query use parameters:

string query = "insert into Sales values(@param1,@param2,@param3,@param4)");
command = new OleDbCommand(query, connect);
command.Parameters.AddWithValue("@param1", Selection);
command.Parameters.AddWithValue("@param2", textBox2.Text);
command.Parameters.AddWithValue("@param3", textBox3.Text);
command.Parameters.AddWithValue("@param4", textBox4.Text);
command.ExecuteNonQuery();
command.Connection = connect;
connect.Close();

I will give you some guidelines how to do it but not provide your complete code:

You need a new column in your gridview and put a button/linkbutton in it as 'Edit/Save/Store' or whatever you want.

When user clicks that button, then ItemCommand Event of Gridview should be fired where you have to get the index of the selected row something like e.Rowindex (You can check the google for detailed tutorial on this) and once you have the index, that is the idea you need. In that event, you should save this data in the database.

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