为什么不能添加一行数据网格视图?

时间:2013-03-13 20:39:07

标签: c# .net datagridview

我有buildgrid方法在表单加载事件中调用它

 private void BuildGrid()
    {



        DataGridViewTextBoxColumn serialtxt = new DataGridViewTextBoxColumn();
        serialtxt.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        serialtxt.Name = "Serial";
        serialtxt.HeaderText = "";
        serialtxt.ReadOnly = true;
        dgv.Columns.Add(serialtxt);


        DataGridViewComboBoxColumn ItemCmp = new DataGridViewComboBoxColumn();
        ItemCmp.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        ItemCmp.Name = "ItemName";
        ItemCmp.HeaderText = "";
        DataSet dsCItemCombo = new DataSet();
        dsCItemCombo = FillItemsCombo();

        BindingSource bindingSourceItem = new BindingSource();
        bindingSourceItem.DataSource = dsCItemCombo;

        ItemCmp.DataSource = bindingSourceItem;
        ItemCmp.DisplayMember = "ItemArabicName";
        ItemCmp.ValueMember = "ItemID";
    //    ItemCmp.SelectedIndex = -1;



    //    clsFunctions.FillComboBox(ref ItemCmp, "Inventory.Items", clsMain.SystemLanguage == clsMain.Langs.Arabic ? "ItemArabicName" : "ItemEnglishName", "ItemID");
        dgv.Columns.Add(ItemCmp);
        ItemCmp.Dispose();
        ////////////////

        DataGridViewComboBoxColumn UnitCmp = new DataGridViewComboBoxColumn();
        UnitCmp.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        UnitCmp.Name = "Unit";
        UnitCmp.HeaderText = "";
        dgv.Columns.Add(UnitCmp);
        UnitCmp.Dispose();
        ////////////////////////////////////////////
        dgv.Columns.Add("Quantity", "");// IX = 3
       // dgv.Rows.Add();
        //dgv.Columns.Add("Discount", "");// IX = 4 
        //dgv.Columns.Add("Total", "");// IX = 4 


        //dgv.Columns["AccountID"].Visible = false;
        //dgv.Columns["BalanceNature"].Visible = false;
        //dgv.Columns["AccountName"].ReadOnly = true;
    }

好的,我有一个叫做视图数据的方法

    private bool ViewData()
    {
        try
        {
            dgv.Rows.Clear();

            oTransHead = new Cls_TransHead();
            oTransDetail = new Cls_TransDetail();

            oTransHead.TransID = clsMain.GetInteger(txtSerial.Text.Trim());
            oTransHead.TransType = transTypeOpenBalance;
            oTransHead.StoreID = clsMain.GetInteger(cmbStore.SelectedValue);
            if (oTransHead.Find())
            {
                dtpDate.Text = oTransHead.TransDate;
                txtDate.Text = oTransHead.TransHijriDate;
                txtNotes.Text = oTransHead.Notes;

                oTransDetail.TransType = oTransHead.TransType;
                oTransDetail.TransID = oTransHead.TransID;
                oTransDetail.StoreID = oTransHead.StoreID;
                if (oTransDetail.Find())
                {

                    dgv.Rows.Add();
                    dgv.SetCellText(dgv.Rows.Count - 1, 0, oTransDetail.Serial.ToString().Trim());
                    dgv.SetCellText(dgv.Rows.Count - 1, 1, oTransDetail.ItemID.ToString().Trim());
                    dgv.SetCellText(dgv.Rows.Count - 1, 2, oTransDetail.UnitID.ToString().Trim());
                    dgv.SetCellText(dgv.Rows.Count - 1, 3, oTransDetail.UnitQty.ToString().Trim());

                }
                dgv.AutoNumberRows();


            }
            else
            {
                //dgv.AutoNumberRows();
                dtpDate.Value = DateTime.Now;
                SwitchingObjects();
                dgv.Rows.Add();


                clsFunctions.SetControlFocus(dtpDate);
            }
         }

在第dgv.Rows.Add()行出现一个异常“名为ItemArabicName的字段不存在” 但数据集携带列的确切名称icannot图在哪里是我的问题

0 个答案:

没有答案