Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a little long but I'll try =)</p> <p>Suppose you do not have additional logic except bringing data to the DataGridView, therefore logic layer functions simply call the data layer functions.<br> Now in this sample we have a form with 2 dgv, one for the order details (readOnly) and the seccond for the lines of a specific order - in this dgv we add a ComboBox for the products And one Button for save.</p> <p>On form load i call this simple func,</p> <pre><code> private void setOrderGridView() { BindingSource orderSrc = new BindingSource(logic.GetOrderDetailsDS(FrmOrder.currentOrderID), "tblOrders"); dgvOrdrdtls.DataSource = orderSrc; var src = logic.GetProductTable(); BindingSource detailSrc = new BindingSource(orderSrc, "orderdetails"); dgvLines.DataSource = detailSrc; DataGridViewComboBoxColumn pr = new DataGridViewComboBoxColumn(); pr.HeaderText = "Product"; pr.Name = "product"; pr.DataSource = src; pr.ValueMember = "pdctId"; pr.DisplayMember = "prdctDsc"; pr.DataPropertyName = "ordrPrdctid"; dgvLines.Columns.Add(pr); } </code></pre> <p>Below you will see the functions <code>GetOrderDetailsDS(..)</code> And <code>GetProductTable()</code><br> All this is for load the dgv's, After you have finished entering or editing the DGV, <code>btnSave_Click(..)</code> call the <code>SaveChanges()</code> func.</p> <p>Hare the DAL code.</p> <pre><code>public class DALimp : iDAL { string connStr; SqlConnection conn; SqlCommand cmd; DataSet ds; SqlDataAdapter da; public DALimp() { connStr = ConfigurationManager.ConnectionStrings["StoreDBConnectionString"].ConnectionString; conn = new SqlConnection(connStr); } public DataSet GetOrderDetailsDS(int oId) { var select = "SELECT * FROM tblOrders WHERE orderId='"+oId+"'"; ds = new DataSet(); cmd = new SqlCommand(select, conn); try { conn.Open(); ds.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges, "tblOrders"); } catch (Exception ex) { throw ex; } finally { conn.Close(); } return ds; } public DataTable GetProductTable() { cmd = new SqlCommand("SELECT * FROM tblProducts", conn); da = new SqlDataAdapter("SELECT * FROM tblorderLines", conn); da.InsertCommand = new SqlCommand("INSERT INTO tblorderLines (ordrNumid,ordrPrdctid,ordrQuantity) Values(@oid,@pid,@qnt)", conn); da.InsertCommand.Parameters.Add("@oid", SqlDbType.Int, 0, "ordrNumid"); da.InsertCommand.Parameters.Add("@pid", SqlDbType.Int, 0, "ordrPrdctid"); da.InsertCommand.Parameters.Add("@qnt", SqlDbType.SmallInt, 0, "ordrQuantity"); try { conn.Open(); ds.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges, "tblProducts"); da.Fill(ds, "tblorderLines"); ds.Relations.Add("orderdetails", ds.Tables["tblOrders"].Columns["orderId"], ds.Tables["tblorderLines"].Columns["ordrNumid"], false); } catch (Exception ex) { throw ex; } finally { conn.Close(); } return ds.Tables["tblProducts"]; } public bool SaveChanges() { try { if (ds.Tables["tblorderLines"].GetChanges() == null) return false; var update = "UPDATE tblorderLines SET ordrNumid= @oni, ordrPrdctid= @opi, ordrQuantity= @oq WHERE ordritmId= @oii"; var cmd = new SqlCommand(update, conn); cmd.Parameters.Add("@oni", SqlDbType.Int, 4, "ordrNumid"); cmd.Parameters.Add("@opi", SqlDbType.Int, 4, "ordrPrdctid"); cmd.Parameters.Add("@oq", SqlDbType.SmallInt, 2, "ordrQuantity"); cmd.Parameters.Add("@oii", SqlDbType.BigInt, 8, "ordritmId").SourceVersion = DataRowVersion.Original; da.UpdateCommand = cmd; da.Update(ds, "tblorderLines"); return true; } catch (Exception ex) { throw ex; } } } </code></pre> <p>Hope i helped and sorry for my english =)</p> <p><strong>Note</strong> There are those who would rather not see any name of a table, cells or relations within the PresentationLayer, so they will add additional functions as needed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload