I'm supposed to create a master-detail form and I must add the details straight on the datagridview which is binded to the database. So I have a form with two tables: intrari (master) and intrari_detaliu (detail). When I use the binding navigator to select a row in table intrari which is the parent table, I also get the corresponding details in table intrari_detaliu. I use text boxes/combox to add value in table intrari. So how do I insert values straight into data grid view? image for the form visual structure
What I tried:
First try:
DataClasses1DataContext db = new DataClasses1DataContext();
private void btnadd_Click(object sender, EventArgs e)
{
int ID; int id_intrari = 0; int id_produs = 0; decimal cantitate = 0; decimal valoare = 0;
for (int i = 0; i <tbl_intrari_detaliuDataGridView.RowCount - 1; i++)
{
if row
ID = Convert.ToInt32(tbl_intrari_detaliuDataGridView.Rows[i].Cells[0].Value.ToString());
id_intrari = Convert.ToInt32(tbl_intrari_detaliuDataGridView.Rows[i].Cells[1].Value.ToString());
id_produs = Convert.ToInt32(tbl_intrari_detaliuDataGridView.Rows[i].Cells[2].Value.ToString());
cantitate = Convert.ToDecimal(tbl_intrari_detaliuDataGridView.Rows[i].Cells[3].Value.ToString());
valoare = Convert.ToDecimal(tbl_intrari_detaliuDataGridView.Rows[i].Cells[4].Value.ToString());
var st = new tbl_intrari_detaliu
{
ID= ID,
id_intrari = id_intrari,
id_produs = id_produs,
cantitate = cantitate,
valoare = valoare,
};
db.tbl_intrari_detalius.InsertOnSubmit(st);
db.SubmitChanges();
}
}
but this first copies every row above and then it adds the new row. E.g.: I already have inserted row A and B, and when I want to insert row C, it first inserts row A and B again and inserts then C.
Second try:
public partial class Intrari2 : Form
{
SqlConnection con;
SqlDataAdapter adap;
DataSet ds;
SqlCommandBuilder cmbl;
private void Intrari2_Load(object sender, EventArgs e)
{
con = new SqlConnection();
con.ConnectionString = @"Data Source=DESKTOP-KVMM566;Initial
Catalog=MyDatabase;Integrated Security=True";
con.Open();
adap = new SqlDataAdapter("select ID,id_intrari,id_produs,cantitate,valoare from tbl_intrari_detaliu", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "tbl_intrari_detaliu");
tbl_intrari_detaliuDataGridView.DataSource = ds.Tables[0];
this.tbl_intrari_detaliuTableAdapter.Fill(this.myDatabaseDataSet.tbl_intrari_detaliu);
this.tbl_IntrariTableAdapter.Fill(this.myDatabaseDataSet.tbl_Intrari);
//Add button------------
private void btnadd_Click(object sender, EventArgs e)
{
cmbl = new SqlCommandBuilder(adap);
adap.Update(ds, "tbl_intrari_detaliu");
//the Fill method doesn't work anymore because of the code above
this.tbl_intrari_detaliuTableAdapter.Fill(this.myDatabaseDataSet.tbl_intrari_detaliu);
}
}
This one updates rows correctly, but when I select a row in table intari, I don't get the corresponding details (according to the id) in table intrari_detaliu. I get the details for all rows in the parent table. I know it's because of the code I added to select the data from table using SqlAdapter, but the code doesn't work without that statement.
So, I need to insert/update/delete a selected row from "intrari_detaliu" and still be able to get the corresponding details when I select a row in the parent table "intrari".
Can you help me, please?













