Yes it is possible. Here is an article on how to use entity framework with windows forms Hope this helps
c# sqlite save to database textboxs and Datagridview inputs
Is it possible to have a Sqlite database that will save information from textboxes and a Datagridview?
I would like to use just one table.
Lets say I have 2 panels one is Textboxes and the other is a Datagridview I save to a database.
When I select it will populate both textboxes and Datagridview, I can save and populate the textboxes but not
sure how to integrate the datagridview. Below is textbox code.
Basically I wont to mix the 2 together.
private void btn_Save_Click(object sender, EventArgs e)
{
var data = new Data();
data.AttendanceData(
dateTimePicker1.Value, tb_Leader_Present.Text, tb_Leader_Online.Text,
tb_Roll.Text, tb_Required_Roll.Text,
tb_Sick.Text, tb_Sick_comm.Text,
tb_Vac.Text, tb_Vac_comm.Text,
tb_Personal.Text, tb_Personal_comm.Text,
tb_Qday.Text, tb_Q_comm.Text,
tb_Late.Text, tb_Late_comm.Text,
tb_Early.Text, tb_Early_comm.Text,
tb_LOA.Text, tb_LOA_comm.Text,
tb_Long_LOA.Text, tb_Long_comm.Text,
tb_Offsite.Text, tb_Offsite_comm.Text,
tb_Other.Text, tb_Other_comm.Text,
tb_Over_Under.Text = (double.Parse(tb_Roll.Text) - double.Parse(tb_Required_Roll.Text)).ToString(),
tb_Total.Text = (double.Parse(tb_Sick.Text) + double.Parse(tb_Vac.Text) +
(double.Parse(tb_Personal.Text) + double.Parse(tb_Qday.Text) +
(double.Parse(tb_Late.Text) + double.Parse(tb_Early.Text) +
(double.Parse(tb_LOA.Text) + double.Parse(tb_Long_LOA.Text) +
(double.Parse(tb_Offsite.Text)
+ double.Parse(tb_Other.Text)))))).ToString());
MessageBox.Show("Saved to DataBase");
}
}
}
3 answers
Sort by: Most helpful
-
-
Timon Yang-MSFT 9,576 Reputation points
2021-01-18T02:55:52.223+00:00 The data in the textbox and the data in the datagridview should come from the same data source.
So we should save the data input of textbox and datagridview to a datatable, and then use the DataAdapter to write the datatable changes to the database.private DataTable dataTable; private void Form1_Load(object sender, EventArgs e) { using (SQLiteConnection connection = new SQLiteConnection(@"Data Source=d:\test\db\hello.db")) { connection.Open(); string selectSql = @"select * from t1"; using (SQLiteCommand command = new SQLiteCommand(selectSql, connection)) { dataTable = new DataTable(); dataTable.Load(command.ExecuteReader()); dataGridView1.DataSource = dataTable; DataRow dataRow = dataTable.Rows[0]; } } } private void Save_Click(object sender, EventArgs e) { using (SQLiteConnection connection = new SQLiteConnection(@"Data Source=d:\test\db\hello.db")) { connection.Open(); using (SQLiteDataAdapter sQLiteDataAdapter = new SQLiteDataAdapter(@"select * from t1", connection)) { SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(sQLiteDataAdapter); sQLiteDataAdapter.Update(dataTable); } } } private void AddToDatatable_Click(object sender, EventArgs e) { DataRow dataRow = dataTable.NewRow(); dataRow["Id"] = textBox1.Text; dataRow["Name"] = textBox2.Text; dataRow["Age"] = textBox3.Text; dataTable.Rows.Add(dataRow); }
This is just a simple example, I hope to give you some ideas, you should add more details when you actually use it.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. -
Kostas 1 Reputation point
2022-03-27T18:07:42.023+00:00 TimonYang
Thanks a lot for sharing this piece of code.
I tried to modify it to my needs but it gives an error that there is no line in position 0.using (SQLiteConnection connection = new SQLiteConnection(@"Data Source=Injection_Settings.db"))
{
connection.Open();
string selectSql = "CREATE TABLE IF NOT EXISTS Description (Mould_Code INTEGER NOT NULL PRIMARY KEY,Supplier TEXT NOT NULL,Colour TEXT NOT NULL,Machine_Type TEXT NOT NULL,Plastic_Type_1 TEXT,Plastic_Type_2 TEXT,Plastic_Type_3 TEXT,Machine_Number INTEGER NOT NULL,Date_Time TEXT NOT NULL);";
using (SQLiteCommand command = new SQLiteCommand(selectSql, connection))
{
dataTable = new DataTable();
dataTable.Load(command.ExecuteReader());
DataRow dataRow = dataTable.Rows[0];
}
}
private void Save_Click(object sender, EventArgs e)
{
using (SQLiteConnection connection = new SQLiteConnection(@"Data Source=Injection_Settings.db"))
{
connection.Open();using (SQLiteDataAdapter sQLiteDataAdapter = new SQLiteDataAdapter(@"SELECT * FROM Description", connection)) { SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(sQLiteDataAdapter); sQLiteDataAdapter.Update(dataTable); } } } private void AddToDatatable_Click(object sender, EventArgs e) { DataRow dataRow = dataTable.NewRow(); dataRow["Mould_Code"] = mould_code_input.Text; dataRow["Supplier"] = supplier_input.Text; dataRow["Colour"] = colour_input.Text; dataRow["Machine_Type"] = machine_type_input.Text; dataRow["Plastic_Type_1"] = listBox1.Text; dataRow["Plastic_Type_2"] = listBox4.Text; dataRow["Plastic_Type_3"] = listBox6.Text; dataRow["Machine_Number"] = machine_number_input.Text; dataRow["Date_Time"] = dateTimePicker1.Text; dataTable.Rows.Add(dataRow);
}