数据未保存在datagrid中 [英] Data not saved in datagrid

查看:104
本文介绍了数据未保存在datagrid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将数据保存在dataGrid中并在2分钟或更长时间后再次重新打开项目时,项目不保存数据,为什么

When i save data in dataGrid and reopen project again after 2 minutes or more project not save data WHY

namespace National_items
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        
        private bool savetag = false;

        private void AdditemBtn_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow row;
                DataTable table = itemsListDataSet.Devlist;
                row = table.NewRow();
                row[0] = textBox1.Text;
                row[1] = textBox2.Text;
                table.Rows.Add(row);
            }
            catch(Exception)
            {
                MessageBox.Show("This Item was inserted", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the ''itemsListDataSet.Devlist'' table. You can move, or remove it, as needed.
            this.devlistTableAdapter.Fill(this.itemsListDataSet.Devlist);
            savetag = true;
        }
        
        private void SavItemBtn_Click(object sender, EventArgs e)
        {
            devlistTableAdapter.Connection.Open();
            devlistTableAdapter.Fill(itemsListDataSet.Devlist);
            itemsListDataSet.GetChanges();
            devlistBindingSource.EndEdit();
            devlistTableAdapter.Connection.Close();
            savetag = true;
        }

        private void DelItemBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        }
       
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (savetag == false)
            { 
                DialogResult dr = MessageBox.Show("Do you want to save changes", "Confirme", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
                if (dr == DialogResult.Cancel)
                    e.Cancel = true;
                else if (dr == DialogResult.Yes)
                    SavItemBtn_Click(sender, e);
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            savetag = false;
        }
    }
}

推荐答案

我想您会在这里找到附件:
如何:将数据集更改保存到数据库 [
问候
Espen Harlinn
I think you will find the asnwer here:
How to: Save Dataset Changes to a Database[^] - this is after all the "official" documentation :)

Regards
Espen Harlinn


对不起,这是我保存btn的原始代码

但是数据更改只保存了一下
当我打开Access数据库时,它已更改
但是片刻之后再次运行项目时,它为空

Sorry this is my original code for save btn

but data changes saved for moments just
when i open access database its changed
but after moments when run project again its empty

<pre lang="cs">private void SavItemBtn_Click(object sender, EventArgs e)<br />
        {<br />
            devlistTableAdapter.Connection.Open();<br />
            itemsListDataSet.GetChanges();<br />
            devlistBindingSource.EndEdit();<br />
            devlistTableAdapter.Update(itemsListDataSet.Devlist);<br />
            devlistTableAdapter.Connection.Close();<br />
            savetag = true;<br />
        }</pre>


在VS中首次运行或调试项目时,它将创建exe并将您的DB和app.config复制到bin目录.对DB的更新已完成到bin中的一个.

您的数据库具有复制到输出目录"属性.如果此属性在程序的后续运行中的值为始终复制",则它将始终用项目中的空DB覆盖数据库.
因此,请检查属性的值并将其更改为请勿复制"或如果更新则复制".

干杯
When you run or debug a project for the first time in VS, it creates the exe AND copies your DB and app.config to the bin directory. Updates to the DB are done to the one in the bin.

Your DB has a property "Copy to Output Directory". If this property has the value "Copy Always" in subsequent runs of your program it always overwrites the DB with the empty one in your project.
So, check the value of the property and change it to "Do not Copy" or "Copy if newer".

Cheers


这篇关于数据未保存在datagrid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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