Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASM
Hi,

I made a test form that contains a DataGridView control. There is also a ToolStrip with a "Save" button on it. Below is the used code:


C#
public partial class TestForm7 : Form
    {
        BindingSource bs;
        DataTable dt;
        SqlDataAdapter da;
        SqlCommandBuilder cb;

        public TestForm7()
        {
            InitializeComponent();
        }

        private void TestForm7_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from Bills", AppConnections.GetSqlConnection());
            dt = new DataTable("Bills");
            cb = new SqlCommandBuilder(da);
            da.Fill(dt);
            bs = new BindingSource();
            bs.DataSource = dt;
            dataGridView1.DataSource = bs;
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            DataRow dr = ((DataRowView)bs.Current).Row;
            DataGridViewCell currentCell = dataGridView1.CurrentCell as DataGridViewCell;
            if (currentCell.IsInEditMode)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
            bs.EndEdit();
            da.Update(dt);
        }
    }


I witness an odd behavior of my DataGridView control that puzzles me.



The following pattern is OK:

The form opens, the first record gets current. The value of a target test cell is “a”. I change the value to “b”. Then without changing the record I move focus to another cell. Then I press ESC, which causes the value of my target test cell change back to its original value of “a”.

The described above is a default behavior in such cases.



The following order of actions causes me troubles:

The form opens, the first record gets current. The value of a target test cell is “a”. I change the value to “b” and click “Save” button. The record gets saved in a database. Then without changing the current cell I enter “c” to the cell and move focus to another cell in the same record. Now I expect that pressing ESC will return “b” as a cell value but that won’t happen!!! I can press ESC how many times I want but that’ll change nothing because for some unknown reason the Row.State changed from Unchanged to Modified.

Why is that? What causes Row.State change is value? That’s abnormal but I don’t know what to do. I need ESC to bring back the original value. Do you have any ideas how to achieve that?

I noticed that if after clicking “Save” button I change the current record and then return back to it carrying out the described above actions then everything will be all right.

Sergiy Vakshul
Posted
Updated 20-Apr-15 12:38pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900