DataGridView 修改某一列,另一列也相应改变

 

总共有三个事件

DgvFeeInspection_CellClick事件

DgvFeeInspection_EditingControlShowing事件

editingControl_TextChanged自定义事件

 

static bool bo = false;
        private void DgvFeeInspection_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DgvFeeInspection.Rows.Count >= 1)
            {
                try
                {
                    if (this.DgvFeeInspection.CurrentCell.ColumnIndex != 4)
                        bo = false;
                    else
                        bo = true;
                }
                catch
                { }
            }
        }

        private void DgvFeeInspection_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (DgvFeeInspection.Rows.Count >= 1)
            {
                if (e.Control.GetType().Equals(typeof(DataGridViewTextBoxEditingControl)))//cell为类TextBox时
                {
                    e.CellStyle.BackColor = Color.FromName("window");
                    DataGridViewTextBoxEditingControl editingControl = e.Control as DataGridViewTextBoxEditingControl;
                    editingControl.TextChanged += new EventHandler(editingControl_TextChanged);
                }
            }
        }
        private void editingControl_TextChanged(object sender, EventArgs e)
        {
            if (DgvFeeInspection.Rows.Count >= 1)
            {
                if (bo == true)
                {
                    string str = DgvFeeInspection.CurrentCell.EditedFormattedValue.ToString();
         

你可能感兴趣的:(WinForm(窗体c#))