Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Bind DataGridView bindingsource.removecurrent associated table row deletion

$
0
0

Dear experts,

i am writing a simple form having DataGridView (dgvInwardEDI) and binding source.

the table InwardEDI is associated to bsinward ( it is a binding source)

in the validating event of dgvInwardEDI if i detect multiple rows having same supplier bill no which is populated upon exit of a billcsv filed of  InwardEDI  table i want to delete that row because it is present else were.

i can delete rows using bsInward.RemoveCurrent() 

but when after moving all rows of gridview i press save button of binding source it gives error columns not meeting not null constraints actually i thing the deleted tows from table do not go away they appear with record signs in column and i suspect that is giving error

can u please how better it could bed done

        private void dgvInwards_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string clmheaderText = dgvInwards.Columns[e.ColumnIndex].Name;
            string clmvalue = e.FormattedValue.ToString();
            if (clmheaderText.Equals("Billcsv"))
            {
                if (String.IsNullOrWhiteSpace(clmvalue))
                {
                    if (DialogResult.Yes == MessageBox.Show("Stop Entering Data ?", "Stop Running program", MessageBoxButtons.YesNo))
                    {
                        // Set the current cell to the cell in column 1, Row e.Rowindex
                        dgvInwards.CurrentCell = dgvInwards.Rows[e.RowIndex].Cells[clmheaderText];
                        bsInward.RemoveCurrent();
                        //e.Cancel = false;
                        //bsInward.CancelEdit();
                        //this.bindNavigator1.btnUndo.PerformClick();
                        this.bindNavigator1.btnSave.PerformClick();
                        return;
                    }
                    else
                    {
                        dgvInwards.Rows[e.RowIndex].ErrorText = "Scan Bar Code Empty?";
                        //dgvGateEntry.Rows[e.RowIndex].Cells["dockey"].ErrorText = "Scan Bar Code Empty ?";
                        e.Cancel = true;
                        return;
                    }
                }
                else
                {
                    decimal assesableamt, bedamt, aedamt, freightamt, vatamt, billamt, vat_rate;
                    string[] rowValue = clmvalue.Split(',');
                    //Check Already Entered
                    //Linq DataGridView Group columns
                    //
                    var rows =
                        (from r in dgvInwards.Rows.OfType<DataGridViewRow>()
                         where (r.Cells["ac_code"].Value == null ? String.Empty : r.Cells["ac_code"].Value).Equals(rowValue[1])&&
                                 (r.Cells["supplierbillno"].Value == null ? String.Empty : r.Cells["supplierbillno"].Value).Equals(rowValue[2])
                         select new
                         {
                             r
                         }).ToList();
                    int recordallowed=0;
                    if (dgvInwards["Billcsv", e.RowIndex].Value==DBNull.Value)
                        recordallowed = 0;
                    else
                        recordallowed = 1;
                    if (rows.Count() > recordallowed)
                    {
                        int inwardid = -1;
                        int.TryParse(rows[0].r.Cells["id"].Value.ToString(),out inwardid);
                        MessageBox.Show("Found @ Id=" + inwardid.ToString());
                        if (DialogResult.Yes == MessageBox.Show("Jump To That Row", "Already At ", MessageBoxButtons.YesNo))
                        {
                            int deleterowpos = bsInward.Position;

                            bsInward.RemoveCurrent();
                            ds.Tables["InwardEDI"].Rows[deleterowpos].Delete();
                            int pos = bsInward.Find("id", inwardid);
                            bsInward.Position = pos;
                        }
                        e.Cancel = true;
                        return;
                    }
                    else
                    {
                        dgvInwards.Rows[e.RowIndex].ErrorText = "";
                        e.Cancel = false;
                    }

                    //
                    string error = "",warnings="";
                    dgvInwards["ac_code", e.RowIndex].Value = rowValue[1]; //VendorCode 1
                    dgvInwards["supplierbillno", e.RowIndex].Value = rowValue[2]; //invoiceNo 2
                    dgvInwards["supplierbilldate", e.RowIndex].Value = rowValue[3]; //invoiceDate 3
                    dgvInwards["pono", e.RowIndex].Value = rowValue[4] + "-" + rowValue[5]; //PONO 4, posrno 5
                    dgvInwards["InwardItem", e.RowIndex].Value = rowValue[6]; //itemid 6
                    dgvInwards["SupplyRate", e.RowIndex].Value = rowValue[7]; //rate 7
                    dgvInwards["inqty", e.RowIndex].Value = rowValue[8];//qty 8
                    dgvInwards["BillTotal", e.RowIndex].Value = rowValue[10];//10-dt.Columns.Add("BillTotal", typeof(decimal));
                    dgvInwards["AssesableValue", e.RowIndex].Value = rowValue[11];//11-dt.Columns.Add("AssesableValue", typeof(decimal));
                    dgvInwards["BED", e.RowIndex].Value = rowValue[12];//12-dt.Columns.Add("BED", typeof(decimal));
                    dgvInwards["AED", e.RowIndex].Value = rowValue[13];//13 - dt.Columns.Add("AED", typeof(decimal));
                    dgvInwards["Freight", e.RowIndex].Value = rowValue[14];//14 - dt.Columns.Add("Freight", typeof(decimal));
                    dgvInwards["CahapterNo", e.RowIndex].Value = rowValue[16];//16 - dt.Columns.Add("CahapterNo", typeof(string));
                    dgvInwards["OCNo", e.RowIndex].Value = rowValue[17];//17 - dt.Columns.Add("ModNo", typeof(string));
                    dgvInwards["inwardemp_id", e.RowIndex].Value = Login.SysUserId;
                    string[] pokey = rowValue[4].ToString().Split('-');
                    if (!pokey.Length.Equals(2))
                    {
                        error += "PO Key Gl-No Not Correct" + '\r' + '\n';
                    }
                    else
                    {
                        string pono = "doc_gl='" + pokey[0] + "' and doc_no='" + pokey[1] +"' and srl='" + rowValue[5] + "'";
                        DataRow[] dRowspo = ds.Tables["po"].Select(pono);
                        if (dRowspo.Length > 0)
                        {
                            if (dRowspo[0]["rate"].ToString() != rowValue[7].ToString())
                            {
                                warnings += "Bill Rate:" +rowValue[7].ToString()+" PO Rate:"+dRowspo[0]["rate"].ToString();
                            }
                            dgvInwards["bed_rt", e.RowIndex].Value = dRowspo[0]["bed_rt"];
                            dgvInwards["SupplierName", e.RowIndex].Value = dRowspo[0]["suppliername"];
                            dgvInwards["InwardItemName", e.RowIndex].Value = dRowspo[0]["itemname"];
                            if (dRowspo[0]["item_no"].ToString() != rowValue[6])
                            {
                                error += "Po Item" + dgvInwards["InwardItem", e.RowIndex].Value +"EDI Item" + rowValue[6] + '\r' + '\n';
                            }
                            Decimal.TryParse(rowValue[11], out assesableamt);
                            Decimal.TryParse(rowValue[12], out bedamt);
                            Decimal.TryParse(rowValue[13], out aedamt);
                            Decimal.TryParse(rowValue[14], out freightamt);
                            Decimal.TryParse(rowValue[15], out vatamt);
                            Decimal.TryParse(rowValue[16], out billamt);

                            //if (aedamt.Equals(0))
                            //{
                            //}
                            if (vatamt.Equals(0))
                            {
                                Decimal.TryParse(dRowspo[0]["SaleTax_rt"].ToString(), out vat_rate);
                                vatamt = Math.Round((assesableamt + bedamt + aedamt + freightamt) * vat_rate / 100, MidpointRounding.AwayFromZero);
                                dgvInwards["vat", e.RowIndex].Value = vatamt;
                            }
                            else
                            {
                                dgvInwards["vat", e.RowIndex].Value = rowValue[15];//15 - dt.Columns.Add("vat", typeof(decimal));
                            }

                        }
                        else
                        {
                            error += "PO Missing" + pokey[0] + "-" + pokey[1] + "-" + rowValue[5] + '\r' + '\n';
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(error))
                    {
                        dgvInwards["errorreport", e.RowIndex].Value = error;
                        DataGridViewRow row = dgvInwards.Rows[e.RowIndex];
                        row.DefaultCellStyle.BackColor = Color.Red;
                    }
                    dgvInwards["warnings", e.RowIndex].Value = warnings;
                    //dgvInwards.EndEdit();

                }

            }
        }


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>