设置dgv是否可编辑(封装DGV)

1.循环遍历
        /// <summary>
        /// 设置药品信息是否可编辑
        /// </summary>
        private void SetDataEdit(ref MzGridControl dgv)
        {
            List<DrugInout> datas = dgv.DataSource as List<DrugInout> ?? new List<DrugInout>();
            dgv.Grid.DisplayLayout.Bands[0].Columns[nameof(DrugInout.DgInfo)].CellActivation = Activation.AllowEdit;
            if (datas != null && dgv.Grid.Rows.Count > 0)
            {
                foreach (var s in dgv.Grid.Rows)
                {
                    if ((bool)s.Cells[nameof(DrugInout.IsNameNoEdit)].Value == true)
                    {
                        s.Cells[nameof(DrugInout.DgInfo)].Activation = Activation.NoEdit;
                    }
                    else
                    {
                        //s.Cells[nameof(DrugInout.DgInfo)].Activation = Activation.AllowEdit;
                        //s.Cells[nameof(DrugInout.DgInfo)].Activation
                        s.Cells[nameof(DrugInout.DgInfo)].Activation = Activation.AllowEdit;
                    }
                }
            }
        }

2.指定列

            //可编辑
            dgvRecordList.Grid.Rows[0].Cells[nameof(DrugInout.DgInfo)].Activation = Activation.AllowEdit;
            //不可编辑
            dgvRecordList.Grid.Rows[0].Cells[nameof(DrugInout.DgInfo)].Activation = Activation.NoEdit;

 3.指定行

//可以编辑(如果指定行可以编辑,但是实际运行不可编辑,必须先指定列可编辑)
dgvMain.Grid.ActiveRow.Cells[nameof(MdvcDeprPrdVo.DdpBeginDt)].Activation = Activation.AllowEdit;

效果图

原文地址:https://www.cnblogs.com/yuanshuo/p/13710545.html