本文介绍了从DataGridview插入新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过表单中的代码实现了TableAdapter-Dataset结构.
我的代码是:


I have implemented TableAdapter-Dataset Structure through code in my form.
My code is:


public partial class frmInspToProdV2 : PPCV1.App_Code.frmBase
    {
        DataSet dsInspProd = new DataSet();
        DataTable tblMst = new DataTable();
        DataTable tblDetails = new DataTable();
        SqlDataAdapter AdaptMst = new SqlDataAdapter();
        SqlDataAdapter ADaptDetails = new SqlDataAdapter();
        BindingSource bsMaster=new BindingSource();
        BindingSource bsDetails=new BindingSource();

        GetAllData gadMst = new GetAllData();
        GetAllData gadDtls = new GetAllData();

        public frmInspToProdV2()
        {
            InitializeComponent();
        }
        private void frmInspToProdV2_Load(object sender, EventArgs e)
        {
            //LoadData();
            InitAdapters();
            InitCommonData();
        }
        private void InitAdapters()
        {
            AdaptMst.SelectCommand = new SqlCommand("SELECT * FROM Iss_Mst WHERE FromDeptID=8 AND ToDeptID<>8 ORDER BY DATE DESC", baseConnection);
            ADaptDetails.SelectCommand = new SqlCommand("SELECT ID,IssMstID,ToOprID,ToDrgId,BatchNo,Qty,FromOprID,Rew,Back FROM Iss_Dtl", baseConnection);
        }

        private void LoadDataV2()
        {
            try
            {
               AdaptMst.SelectCommand = new SqlCommand("SELECT * FROM Iss_Mst WHERE FromDeptID=8 AND ToDeptID<>8 ORDER BY DATE DESC", baseConnection);
                baseConnection.Open();
                AdaptMst.Fill(tblMst);
                dsInspProd.Tables.Add(tblMst);
                dsInspProd.Tables[0].TableName = "tblMst";
                dsInspProd.Tables[0].Columns["ID"].AutoIncrement = true;
                dsInspProd.Tables[0].Columns["ID"].Unique = true;
                GetDetailsData();

                InitCommonData();
                SetData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                baseConnection.Close();
            }
        }
        private void GetMasterData()
        {
            try
            {
                tblMst = gadMst.GetByQuery("SELECT * FROM Iss_Mst WHERE FromDeptID=8 AND ToDeptID<>8 ORDER BY DATE DESC", ref tspbBase,ref tsBaseLabel);
                dsInspProd.Tables.Add(tblMst);
                dsInspProd.Tables[0].TableName = "tblMst";
                dsInspProd.Tables[0].Columns["ID"].AutoIncrement = true;
                dsInspProd.Tables[0].Columns["ID"].Unique = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void GetDetailsData()
        {
            try
            {
                ADaptDetails.Fill(dsInspProd, "tblDetails");
                dsInspProd.Tables.Add(tblDetails);
                dsInspProd.Tables[1].TableName = "tblDetails";
                dsInspProd.Tables[1].Columns["ID"].AutoIncrement = true;
                dsInspProd.Tables[1].Columns["ID"].Unique = true;

                ADaptDetails.TableMappings.Add(tblDetails.TableName, dsInspProd.Tables[1].TableName);
                dsInspProd.EnforceConstraints = false;
                DataRelation dr = new DataRelation("IssDtl", dsInspProd.Tables[0].Columns["ID"], dsInspProd.Tables[1].Columns["IssMstID"]);
                dr.Nested = true;
                dsInspProd.Relations.Add(dr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void InitCommonData()
        {
            DataView dvToOperations = new DataView(Operations.Table);
            DataView FromDrawings = new DataView(Drawings.Table);
            DataView RcvdBy = new DataView(Employees.Table);
            DataView ToDepts = new DataView(Departments.Table);

            cmbFromDept.DataSource = Departments;
            cmbFromDept.DisplayMember = "Name";
            cmbFromDept.ValueMember = "ID";

            cmbToDept.DataSource = ToDepts;
            cmbToDept.DisplayMember = "Name";
            cmbToDept.ValueMember = "ID";

            cmbIssBy.DataSource = Employees;
            cmbIssBy.DisplayMember = "FullName";
            cmbIssBy.ValueMember = "ID";

            cmbRcvdBy.DataSource = RcvdBy;
            cmbRcvdBy.DisplayMember = "FullName";
            cmbRcvdBy.ValueMember = "ID";

            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Operation"]).DataPropertyName = "ToOprID";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Operation"]).DisplayMember = "Name";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Operation"]).ValueMember = "ID";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Operation"]).DataSource = dvToOperations;

            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Drawing"]).DataPropertyName = "ToDrgID";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Drawing"]).DisplayMember = "Code";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Drawing"]).ValueMember = "ID";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["Drawing"]).DataSource = Drawings;

            ((DataGridViewTextBoxColumn)dgvInspProd.Columns["BatchNo"]).DataPropertyName = "BatchNo";
            ((DataGridViewTextBoxColumn)dgvInspProd.Columns["Qty"]).DataPropertyName = "Qty";

            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["FromOperation"]).DataPropertyName = "FromOprID";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["FromOperation"]).DisplayMember = "Name";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["FromOperation"]).ValueMember = "ID";
            ((DataGridViewComboBoxColumn)dgvInspProd.Columns["FromOperation"]).DataSource = Operations;
        }
        private void SetData()
        {
            bsMaster.DataSource = dsInspProd;
            bsMaster.DataMember = "tblMst";
            bsMaster.AllowNew = true;
            bsDetails.DataSource = bsMaster;
            bsDetails.DataMember = "IssDtl";
            bsDetails.AllowNew = true;

            bnBase.BindingSource = bsMaster;
            txtSlipNo.DataBindings.Add("Text", bnBase.BindingSource, "SlipNo");
            dtpDate.DataBindings.Add("Text", bnBase.BindingSource, "Date");


            cmbFromDept.DataBindings.Add("SelectedValue", bnBase.BindingSource, "FromDeptID");
            cmbFromDept.Enabled = false;
            cmbToDept.DataBindings.Add("SelectedValue", bnBase.BindingSource, "ToDeptID");
            cmbIssBy.DataBindings.Add("SelectedValue", bnBase.BindingSource, "IssBy");
            cmbRcvdBy.DataBindings.Add("SelectedValue", bnBase.BindingSource, "RcvdBy");

            dgvInspProd.AutoGenerateColumns = false;
            dgvInspProd.DataSource = bsDetails;

        }
        private void dgvInspProd_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {}
                protected override void tsbtnBaseAdd_Click(object sender, EventArgs e)
        {
            base.tsbtnBaseAdd_Click(sender, e);
            try
            {

                SqlCommandBuilder InsMst = new SqlCommandBuilder(AdaptMst);
                AdaptMst.InsertCommand = InsMst.GetInsertCommand();
                dsInspProd.AcceptChanges();
            }
            catch (Exception ex)
            {
                tsBaseLabel.Text = ex.Message.ToString();
            }
        }
        protected override void tsbtnBaseDel_Click(object sender, EventArgs e)
        {
            base.tsbtnBaseDel_Click(sender, e);
        }
        protected override void tsbtnBaseSave_Click(object sender, EventArgs e)
        {
            base.tsbtnBaseSave_Click(sender, e);
            try
            {
                //dsInspProd.AcceptChanges();
                SqlCommandBuilder Updt = new SqlCommandBuilder(AdaptMst);
                AdaptMst.UpdateCommand = Updt.GetUpdateCommand();

                AdaptMst.Update(dsInspProd, "tblMst");
                SqlCommandBuilder UpdtDetails = new SqlCommandBuilder(ADaptDetails);
                ADaptDetails.UpdateCommand = UpdtDetails.GetUpdateCommand();
                //ADaptDetails.Update(dsInspProd, "tblDetails");

                SqlCommandBuilder InsertDetails = new SqlCommandBuilder(ADaptDetails);
                ADaptDetails.InsertCommand = InsertDetails.GetInsertCommand();

                ADaptDetails.Update(dsInspProd, "tblDetails");

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
                tsBaseLabel.Text = ex.Message.ToString();
            }
        }
        protected override void tsbtnBaseRetrieve_Click(object sender, EventArgs e)
        {
            base.tsbtnBaseRetrieve_Click(sender, e);
            LoadDataV2();
        }
        protected override void tsbtnBaseEdit_Click(object sender, EventArgs e)
        {
            base.tsbtnBaseEdit_Click(sender, e);
        }
        protected override void tsbtnBaseEdit_MouseUp(object sender, MouseEventArgs e)
        {
            base.tsbtnBaseEdit_MouseUp(sender, e);
        }
        protected override void tsbtnBaseEdit_CheckedChanged(object sender, EventArgs e)
        {
        }
        protected override void bsBase_CurrentItemChanged(object sender, EventArgs e)
        {
            base.bsBase_CurrentItemChanged(sender, e);
        }

    }



我的问题是无法从DataGridView中插入...

它给出了一个错误..
违反主键约束....无法在对象dbo.issdtl中插入重复的键...



My Prob is Insertion is not possible from DataGridView...

it gives an error..
Violation of Primary key constraint .... Cannot insert duplicate key in object dbo.issdtl...

推荐答案


这篇关于从DataGridview插入新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:08