Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Something caused Row.State change from Unchanged to Modified.

$
0
0

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:

    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);
            dr.AcceptChanges();
        }
    }

I witness an odd behavior of myDataGridView 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




Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>