Example code:
public void GetShipmentsByYearAndTICType()
{
ShipDataContext dc = new ShipDataContext();
var q = from t in dc.TICs
join a in dc.ArrivalEUs on t.ArrivalEUID equals a.ArrivalEUID
join d in dc.Departments on t.DepartmentID equals d.DepartmentID
join i in dc.InWarehouses on t.InWarehouseID equals i.InWarehouseID
join s in dc.ShippingMethods on t.ShippingMethodID equals s.ShippingMethodID
join x in dc.ExWarehouses on t.ExWarehouseID equals x.ExWarehouseID
join tt in dc.TICTypes on t.TICTypeID equals tt.TICTypeID
where t.Year.Equals(Convert.ToInt32(comboBoxYear.Text)) && tt.TICType1.Equals(comboBoxTICType.Text)
select new
{
t.NR,
t.Year,
tt.TICType1,
t.USR,
t.ShippingDocument,
d.Department1,
t.TICRef,
t.Received,
s.ShippingMethod1,
x.ExWarehouse1,
i.InWarehouse1,
t.Carrier,
t.BillOfLading,
t.ETA,
a.ArrivalEU1,
t.MRNnr,
t.Pieces,
t.Weight,
t.Incoterms,
t.Invoices,
t.FreightSupplier,
t.FreightReference,
t.FreightCosts,
t.InlandHauleSupplier,
t.InlandHauleReference,
t.InlandHauleCosts,
t.AdminInlandSupplier,
t.AdminInlandReference,
t.AdminInlandCosts,
t.DutiesSupplier,
t.DutiesReference,
t.DutiesCosts,
t.IMZ,
t.RectificationIMZ,
t.Remarks
};
dataGridView1.DataSource = q;
}
I want to be able to Double click a record in the datagridview and edit this in an exisiting
Windows form. This form is also used to insert new records to the SQL DB.
Example code:
private void AddNewTICShipment()
{
//Data maping object to our database
ShipDataContext dc = new ShipDataContext();
TIC objTIC = new TIC();
//TICTypeID default value for Cleared Tics
objTIC.TICTypeID = 2;
objTIC.Year = Convert.ToInt32(textBoxYear.Text);
objTIC.ShippingDocument = textBoxShippingDocument.Text;
if (String.IsNullOrEmpty(textBoxTICRef.Text))
{
objTIC.TICRef = null;
}
else
{
objTIC.TICRef = Convert.ToInt32(textBoxTICRef.Text);
}
string receivedDate = dateTimePickerReceived.Value.ToShortDateString();
objTIC.Received = receivedDate;
objTIC.Carrier = textBoxCarrier.Text;
objTIC.BillOfLading = textBoxBillOfLading.Text;
string ETADate = dateTimePickerETA.Value.ToShortDateString();
objTIC.ETA = ETADate;
if (String.IsNullOrEmpty(textBoxPieces.Text))
{
objTIC.Pieces= null;
}
else
{
objTIC.Pieces = Convert.ToInt32(textBoxPieces.Text);
}
objTIC.Incoterms = textBoxIncoterms.Text;
objTIC.Invoices = textBoxInvoices.Text;
objTIC.IMZ = textBoxIMZ.Text;
objTIC.RectificationIMZ = textBoxRectificationIMZ.Text;
objTIC.Remarks = richTextBoxRemarks.Text;
objTIC.FreightSupplier = textBoxFreightSupplier.Text;
objTIC.FreightReference = textBoxFreightReference.Text;
objTIC.InlandHauleSupplier = textBoxInlandHauleSupplier.Text;
objTIC.InlandHauleReference = textBoxInlandHauleReference.Text;
objTIC.AdminInlandSupplier = textBoxAdminInlandSupplier.Text;
objTIC.AdminInlandReference = textBoxAdminInlandReference.Text;
objTIC.DutiesSupplier = textBoxDutiesSupplier.Text;
objTIC.DutiesReference = textBoxFreightReference.Text;
string arrivalEU = comboBoxArrivalEU.SelectedValue.ToString();
switch (arrivalEU)
{
case "BRU":
objTIC.ArrivalEUID = 1;
break;
case "ANR":
objTIC.ArrivalEUID = 2;
break;
case "RTN":
objTIC.ArrivalEUID = 3;
break;
}
string department = comboBoxDepartment.SelectedValue.ToString();
switch (department)
{
case "IRRI":
objTIC.DepartmentID = 1;
break;
case "IRRI/DE":
objTIC.DepartmentID = 2;
break;
case "SP":
objTIC.DepartmentID = 3;
break;
}
string shippingMethod = comboBoxShippingMethod.SelectedValue.ToString();
switch (shippingMethod)
{
case "R":
objTIC.ShippingMethodID = 1;
break;
case "O":
objTIC.ShippingMethodID = 2;
break;
case "A":
objTIC.ShippingMethodID = 3;
break;
case "C":
objTIC.ShippingMethodID = 4;
break;
}
string exWarehouse = comboBoxExWarehouse.SelectedValue.ToString();
switch (exWarehouse)
{
case "PLY":
objTIC.ExWarehouseID = 1;
break;
case "ELP":
objTIC.ExWarehouseID = 2;
break;
case "TOM":
objTIC.ExWarehouseID = 3;
break;
}
string inWarehouse = comboBoxInWarehouse.SelectedValue.ToString();
switch (inWarehouse)
{
case "OEV":
objTIC.InWarehouseID = 1;
break;
case "ESS":
objTIC.InWarehouseID = 2;
break;
}
//Converting Decimal . to , for Weight, Freight, Inland, adminInland and duties
string weightValue = textBoxWeight.Text;
string freightValue = textBoxFreightCosts.Text;
string inlandValue = textBoxInlandHauleCosts.Text;
string adminInlandValue = textBoxAdminInlandCosts.Text ;
string dutiesValue = textBoxDutiesCosts.Text;
{
if (weightValue.Contains(".") | freightValue.Contains(".") | inlandValue.Contains(".") | adminInlandValue.Contains(".") )
weightValue = weightValue.Replace(".", ",");
freightValue = freightValue.Replace(".", ",");
inlandValue = inlandValue.Replace(".", ",");
adminInlandValue = adminInlandValue.Replace(".", ",");
dutiesValue = dutiesValue.Replace(".", ",");
}
if (String.IsNullOrEmpty(textBoxWeight.Text) )
{
objTIC.Weight = null;
}
else
{
objTIC.Weight = Convert.ToDecimal(weightValue);
}
if (String.IsNullOrEmpty(textBoxFreightCosts.Text))
{
objTIC.FreightCosts = null;
}
else
{
objTIC.FreightCosts = Convert.ToDecimal(freightValue);
}
if (String.IsNullOrEmpty(textBoxInlandHauleCosts.Text))
{
objTIC.InlandHauleCosts = null;
}
else
{
objTIC.InlandHauleCosts = Convert.ToDecimal(inlandValue);
}
if (String.IsNullOrEmpty(textBoxAdminInlandCosts.Text))
{
objTIC.AdminInlandCosts = null;
}
else
{
objTIC.AdminInlandCosts = Convert.ToDecimal(adminInlandValue);
}
if (String.IsNullOrEmpty(textBoxDutiesCosts.Text))
{
objTIC.DutiesCosts = null;
}
else
{
objTIC.DutiesCosts = Convert.ToDecimal(dutiesValue);
}
dc.TICs.InsertOnSubmit(objTIC);
dc.SubmitChanges();
}
Whats the best way to do this?
Another option can be to make a new windows form if that's easier? Some of the values from the existing form are from a combobox, so I think it will be better to use the existing form where i also insert new records in?
Replies
Know the answer? Post it — somebody with the same question will find it here.