C#sqlite dataGridView交互复制数据 [英] C# sqlite dataGridView interacting duplicates the data

查看:200
本文介绍了C#sqlite dataGridView交互复制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..



目前正在使用sqlite编写数据库软件,并且出现了这个问题。



http://i60.tinypic.com/1555hs9.png [ ^ ]



一开始,数据表为空我输入Foo作为名称,输入Bar作为项目,当我再次将数据表加载到数据网格视图中时,它包含另一个Foo人,而没有我输入Item列的值。



这是代码..



Hi..

currently im writing a database software using sqlite and this problem occurred.

http://i60.tinypic.com/1555hs9.png[^]

at the beginning, the datatable was empty and I typed "Foo" as the name and "Bar" as the Item and when I load the datatable again into the data grid view it contains another "Foo" person without the value I entered into the Item column.

Here's the codes..

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;

            // retrieveing the ID cell of the changed row
            DataGridViewCell IDcell = dataGridView1["ID", row];

            // check if selected row is an existing row or new one
            bool newRow = false;
            if (IDcell.Value.ToString().Length == 0)
                newRow = true;
            else
                newRow = false;


            if (con.State != ConnectionState.Open)
                con.Open();

            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = con;

            if (newRow)
            {
                string sql = @" insert into myTable(Name, Item)
                                values(@name, @item)";

                cmd.CommandText = sql;

                // setting parameters                
                cmd.Parameters.AddWithValue("@name", dataGridView1["Name", row].Value.ToString());
                cmd.Parameters.AddWithValue("@item", dataGridView1["Item", row].Value.ToString());
            }
            else
            {
                string sql = @" update myTable 
                                set
                                Name = @name,
                                Item = @item
                                where ID = @id";

                cmd.CommandText = sql;

                // setting parameters
                cmd.Parameters.AddWithValue("@name", dataGridView1["Name", row].Value.ToString());
                cmd.Parameters.AddWithValue("@item", dataGridView1["Item", row].Value.ToString());
                cmd.Parameters.Add("@id", DbType.Int32).Value = int.Parse(dataGridView1["ID", row].Value.ToString());
            }

            cmd.ExecuteNonQuery();

            con.Close();
        }





我的代码出了什么问题?

我不想要一些没有意义的Foo没有吧。



提前谢谢!



what's the wrong with my code ?
I dont want some meaningless Foo without the Bar.

Thanks in advance !

推荐答案

你的逻辑是非常非常有缺陷的。



只要任何单元格的值发生变化,您的代码就会执行。好的。小心。



您正在检查更改行的名为ID的DGV列的值。 (说真的,如果你让用户访问你桌子的PrimaryKey列,你就会犯一个巨大的错误!随后会出现一个损坏的数据集。)如果这个ID单元格没有值(这是一种可怕的检查方式)顺便提一下!),执行数据库插入。 OK。



在下次调用CellValueChanged时,再次检查DGV以获取该行的ID。嗯...即使您在最后一次调用CellValueChanged时将数据写入数据库,也会有ID值。



为什么?因为您从未从数据库重新加载DataTable以从数据库获取更新的ID值,并且永远不会将网格反弹到此新数据。仅仅因为您向数据库写入值并不意味着DGV知道数据库已更改。它没有!它只是查看你绑定的DataTable,甚至DataTable也不知道数据库发生了变化。





你为什么要尝试在每次更改单元格时将数据写入数据库?这是绝对的要求吗?记录不完整可以接受吗?如果用户在记录的一个字段中输入数据怎么办?是否可以接受该记录的所有其他字段为空?
Your logic is very, very flawed.

Your code is executing whenever the value of any cell changes. OK, fine. Be careful.

You're checking the value of the DGV column called "ID" for the row that changed. (Seriously, if you're giving the user access to the PrimaryKey column of your table you're making a HUGE mistake! A corrupted dataset will ensue.) If this ID cell doesn't have a value (that's a terrible way to check for a value by the way!), execute a database insert. OK.

On the next call to CellValueChanged, you again check the DGV for an ID for that row. Hmmm... there's ID value even though you wrote the data to the database on the last call to CellValueChanged.

Why? Because you never reloaded the DataTable from the database to get the updated ID value from the database and never rebound the grid to this new data. Just because you write a value to the database doesn't mean that the DGV knows that the database changed. It doesn't! It's only looking at the DataTable you bound it to and even the DataTable doesn't know the database changed.


Why are you trying to write data to the database on every cell change? Is this an absolute requirement? Is it acceptable to have incomplete records? What if the user enters data in one field of a record? Is it acceptable that ALL OTHER FIELDS of that record are null?


这篇关于C#sqlite dataGridView交互复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆