samedi 14 février 2015

How to use SQLiteDataAdapter to insert records with header columns

I'm a bit of a newbie to C# so bear with me and my horribad code...


I am developing a VS2013 Forms application using SQLite.


Within my form I have a few "header" fields, and down below it a DataGridView bound to a dataset using the below code.The header fields determine the dataset to be loaded using the SQL select statement below.



private void Grid_Init()
{
Open_VCS_Connection();
string sql_query_main = "SELECT * FROM VCS_DATA WHERE OPERATOR ='" + operator_comboBox.Text.ToString() + "' AND ROTATION = '" + rotation_comboBox.Text.ToString() + "' AND ENTRY_DATE = '" + entry_dateTimePicker.Value.ToShortDateString() + "'";
main_ds = new DataSet();
main_da = new SQLiteDataAdapter(sql_query_main, dbConnection);
sqlcmdBuilder = new SQLiteCommandBuilder(main_da);
main_ds.Reset();
main_da.Fill(main_ds);
main_dataGridView.DataSource = main_ds.Tables[0].DefaultView;
main_dataGridView.Columns[0].Visible = false; //record_id
main_dataGridView.Columns[1].Visible = false; //operator
main_dataGridView.Columns[2].Visible = false; //rotation
main_dataGridView.Columns[3].Visible = false; //entry_date
main_dataGridView.Columns[4].Visible = false; //created_date
main_dataGridView.Columns[5].Visible = false; //updated_date
Close_VCS_Connection();
}


This populates my data grid view just fine and allows me to insert records using the main_da.Update(main_ds) method for the dataset which I have attached to a "Save Button". However the problem is that it does not populate the hidden columns. It also throws errors when I try to delete too but that is another problem.


I have viewed numerous tutorials on SQLiteDataAdapter, and DataGridView, Dataset, but I can't quite wrap my head around how to get it to do an insert populating the hidden columns with text from my text boxes which serve as my header information for my datagrid.


And I have not been able to find any really good code examples that do something like this short of making a form specifically for data entry. I'd rather the user be able to work directly in DGV.


Aucun commentaire:

Enregistrer un commentaire