angel

angel

  • NA
  • 1
  • 0

cant clear EditedFormattedValue

Jun 19 2009 8:31 PM

hi i got a question related to the datagridview cellvalue and celleditedformatedvalue. What i want to do is
i try key in the product code to my first column by keyboard at first - this works fine cos is in edit mode and the value is null.  However when i change the product code again by using F2, what trouble me is the EditedFormattedValue which i key in at first by keyboard is still there. As such, it enters into the both" if statement" below:
How can i prevent it by clearing the editedformattedvalue?

 

 private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Add("c1", "c1");
            this.dataGridView1.Columns.Add("c2", "c2");
            this.dataGridView1.Rows.Add();
            this.dataGridView1.CellValidating +=
                new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
        }

        void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if(this.dataGridView1.CurrentCell.EditedFormattedValue!=null)
                {
                    Console.WriteLine("CurrentCell.EditedFormattedValue" + this.dataGridView1.CurrentCell.EditedFormattedValue.ToString());
                }
                if (this.dataGridView1.CurrentCell.Value != null)
                {
                    Console.WriteLine("CurrentCell.Value " + this.dataGridView1.CurrentCell.Value.ToString());
                }
                Console.WriteLine("");
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F2)
            {
                Form2 f = new Form2();
                f.ShowDialog();
                this.dataGridView1.CurrentCell.Value = f.Code;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }