首页 新闻 赞助 找找看

winfrom奇怪的问题!!高手进

0
悬赏园豆:5 [已关闭问题] 关闭于 2012-12-23 16:53

1.后台load时间中代码:

   public Form2()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form2_Load);
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            DataRow drTemp;
            drTemp = dt.NewRow();
            drTemp[0] = "Jordan";
            drTemp[1] = 10;
            dt.Rows.Add(drTemp);
            drTemp = dt.NewRow();
            drTemp[0] = "KOBE";
            drTemp[1] = 20;
            dt.Rows.Add(drTemp);
            dt.AcceptChanges();
            mBindingSource = new BindingSource();
            mBindingSource.DataSource = dt;
            this.dataGridView1.DataSource = mBindingSource;
            this.textBox1.DataBindings.Add("Text", mBindingSource, "Name",false,  DataSourceUpdateMode.OnPropertyChanged);
            this.textBox2.DataBindings.Add("Text", mBindingSource, "Age", false, DataSourceUpdateMode.OnPropertyChanged);

 保存按钮双击事件中:

 if (((DataTable)mBindingSource.DataSource).GetChanges() == null)
                {
                    MessageBox.Show("没有修改到数据!");
                }
                else
                {
                    DataRow dr = ((DataRowView)mBindingSource.Current).Row;
                    ((DataTable)mBindingSource.DataSource).AcceptChanges();
                }

为什么前台在textbox中输入其它值后直接按保存按钮。。会提示没有修改数据呢!!!!????

彬彬@科比的主页 彬彬@科比 | 初学一级 | 园豆:43
提问于:2012-12-22 22:17
< >
分享
所有回答(3)
0

 if (((DataTable)mBindingSource.DataSource).GetChanges() == null)

判断有问题。

悟行 | 园豆:12559 (专家六级) | 2012-12-23 11:42
0

嗯,比保存以后是不是在数据源中有那行数据呢,我没有看见你的增加,根据情况,你可以看看那个数据源是不是已经有了你的添加行。

代码苦行僧 | 园豆:311 (菜鸟二级) | 2012-12-23 16:19
0

自己已搞定

彬彬@科比 | 园豆:43 (初学一级) | 2012-12-23 16:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册