首先我用datagridview绑定一个datatable,然后使用代码修改其中一项的值,但我取这项的当前值还是以前的值,也就是
dt.Rows[0]["column1", DataRowVersion.Original] 等于 dt.Rows[0]["column1", DataRowVersion.Current]。如果我取dt.Rows[0]["column1", DataRowVersion.Current]以前先点下datagridview,让datagridview获得焦点,就没有问题。不可能每次修改完值后,还点下datagridview,这个问题怎么解决?
后面是具体的代码

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            private DataTable dt = new DataTable();        public Form1()
            {
                InitializeComponent();
                dataGridView1.AutoGenerateColumns = false;
            }        private void Form1_Load(object sender, EventArgs e)
            {
                dt.Columns.Add("column1");
                DataRow newRow = dt.NewRow();
                newRow["column1"] = "a";
                dt.Rows.Add(newRow);
                dt.AcceptChanges();            dataGridView1.DataSource = dt;
            }        private void button1_Click(object sender, EventArgs e)
            {
                string oldValue = dt.Rows[0]["column1", DataRowVersion.Original].ToString();
                string curValue = dt.Rows[0]["column1", DataRowVersion.Current].ToString();            MessageBox.Show(oldValue + " " + curValue);
                //这里为什么老的、新的都是a
                //如果先点下datagridview1,相当于让datagridview获得焦点,这样就可以获得新的值,为什么
            }        private void textBox1_TextChanged(object sender, EventArgs e)
            {
                dataGridView1.Rows[0].Cells["column1"].Value = textBox1.Text;
            }
        }
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
      

  2.   

    namespace WindowsFormsApplication3
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.button1 = new System.Windows.Forms.Button();
                this.textBox1 = new System.Windows.Forms.TextBox();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.AllowUserToAddRows = false;
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1});
                this.dataGridView1.Location = new System.Drawing.Point(12, 12);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.Size = new System.Drawing.Size(240, 150);
                this.dataGridView1.TabIndex = 0;
                // 
                // Column1
                // 
                this.Column1.DataPropertyName = "column1";
                this.Column1.HeaderText = "Column1";
                this.Column1.Name = "Column1";
                this.Column1.ReadOnly = true;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 200);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(152, 200);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 21);
                this.textBox1.TabIndex = 2;
                this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(367, 322);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        }
    }
      

  3.   

    用 BindingSource 双向绑定呀。这样你就不用在 TextChanged 里写赋值了。http://blog.csdn.net/fangxinggood/archive/2008/04/18/2304047.aspx
      

  4.   

    如果不愿意改代码,你可以把 DataGridView 的 EditMode 改为 EditProgrammatically
      

  5.   

    非常感谢fangxinggood的回复,我改了下程序
    1.在dataGridView1.DataSource = dt;这句后面加上
      textBox1.DataBindings.Add("Text", dt, "column1");
    2.按钮单击事件改成
    string oldValue = dt.Rows[0]["column1", DataRowVersion.Original].ToString();
    string curValue = dt.Rows[0]["column1", DataRowVersion.Current].ToString();
    string value = dt.Rows[0]["column1"].ToString();
    MessageBox.Show(oldValue + " " + curValue + " " + value + " " + dt.Rows[0].RowState.ToString());为什么这里的curValue还是原值,value倒是我修改的值,而且rowstate是unchanged,是不是我绑定的方法有问题?
      

  6.   

    我看了下RowState的介绍,当等于Unchanged时,默认值是Current,那为什么dt.Rows[0]["column1", DataRowVersion.Current]和dt.Rows[0]["column1"]不等
      

  7.   

    winform的dataBinding后面其实做了很多事,
    其实DataGridView绑定的是DataTable的DataView
    Button 按下时,你可以通过下面的语句获得当前绑定的行:var row = BindingContext[dataGridView1.DataSource].Current as DataRowView;当你在TextBox修改值,然后按下Button,你会发现 row.IsEdit == true,
    且DataGridView里的值未发生变化。它标明编辑还没有结束。你可以在Button按下时,调用:row.EndEdit();告诉DataGridView编辑结束了就OK。
      

  8.   

    其实DataGridView绑定的是DataTable的DataView这句话不妥先不要考虑。
      

  9.   

    非常感谢fangxinggood,不但解决了我的问题,还让我学到不少,qqq