Monday, 18 March 2013

Multiple data Insert from gridview using datatable in Window Application





C# Code
 private void FrmKitSetUp_Load(object sender, EventArgs e)
        {
            CreateDataTable();                     
        }
private void CreateDataTable()
        {
            try
            {
                dt = new DataTable();
                dt.Columns.Add("SlNo", typeof(int));
                dt.Columns.Add("Barcode", typeof(string));
                dt.Columns.Add("Quantity", typeof(int));
                dt.Columns.Add("Item_id", typeof(int));
            }
            catch (Exception ex)
            {
            }
        }
  private void BtnSave_Click(object sender, EventArgs e)
        {
            if (checkData() == true)
            {
                if (checkDuplicateSave() == true)
                {
                    if (MessageBox.Show("Do you want to save this record?", "kenCloud", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.Yes)
                    {
                        CreateDataTable_Insert();
                        TAGGING_Setup objIssueQC = new TAGGING_Setup();
                        // objIssueQC.Issue_NO_QC = "1";
                        objIssueQC.Pack_Title = TxtTitle.Text.Trim();
                        objIssueQC.Barcode = CmbBarcode.Text.Trim();
                        objIssueQC.Created_By = 1;
                        objIssueQC.KitDetails = dt1;
                        ERPManagement.GetInstance.InsertKitting(objIssueQC);
                        MessageBox.Show("Data saved successfully.", "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Reset();
                    }
                }
            }
        }

 private void CreateDataTable_Insert()
        {
            try
            {
                dt1 = new DataTable();
                dt1.Columns.Add("Barcode", typeof(string));
                dt1.Columns.Add("Quantity", typeof(int));
                dt1.Columns.Add("Item_id", typeof(int));

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt1.Rows.Add(new Object[] { dt.Rows[i][1].ToString(), int.Parse(dt.Rows[i][2].ToString()), int.Parse(dt.Rows[i][3].ToString()) });
                    dt1.AcceptChanges();
                    dt1.DefaultView.Sort = "Barcode";
                }
            }
            catch (Exception ex)
            {
            }
        }
//Grid re add Heba
  private void BtnAdd_Click(object sender, EventArgs e)
        {
                       if (int.Parse(TxtQuantity.Text) <= quantity)
                    {
                        EditDataTable();

                    }

       }
   private void EditDataTable()
        {
            if (DataTableEditFlag == 2)
            {
                if (InsertIntoGridAtEdit() == true)
                {
                    if (rowindex == DgvBarcode.CurrentRow.Index)
                    {
                        dt.Rows[rowindex]["Barcode"] = CmbBarcode.Text;
                        dt.Rows[rowindex]["Quantity"] = TxtQuantity.Text;
                        dt.AcceptChanges();
                        DgvBarcode.DataSource = dt;
                        DataTableEditFlag = 1;
                        GetSlnoDtTbl(dt, TxtSlno);
                        ClearBarcode();
                    }
                    else
                    {
                        MessageBox.Show("You have select a wrong barcode to edit.", "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        // ClearBarcode();
                        CmbBarcode.Focus();
                        return;
                    }
                }
            }}

No comments:

Post a Comment