using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; namespace WindowsApplication6 

    public partial class FacilityInfo : Form 
    { 
        private SqlConnection MyConn = new SqlConnection("server=PC267003173732;Trusted_Connection=yes;user=sa;pwd=admin;database=storage"); 
        private DataTable FacNumInfo = new DataTable(); 
        private DataRow numRow; 
        private CurrencyManager FNReport; 
        private SqlTransaction FacNumTran;         public FacilityInfo() 
        { 
            InitializeComponent(); 
        }         private void FacilityInfo_Load(object sender, EventArgs e) 
        { 
            DataSet FacNumset = new DataSet(); 
            FacilityInfoAdapter.Fill(FacNumInfo); 
            this.dataGrid1.DataSource = FacNumInfo.DefaultView; 
            FNReport = (CurrencyManager)BindingContext[FacNumInfo]; 
            DataGridTableStyle fn = new DataGridTableStyle(); 
            this.DataBindingsFunction(); 
            fn.MappingName = FacNumInfo.TableName; 
            this.dataGrid1.Select(0);//select the first column 
            
        } 
        private void DataBindingsFunction()//Binding 
        { 
            this.textNum.DataBindings.Add("Text", FacNumInfo, "FacilityNum"); 
            this.textName.DataBindings.Add("Text", FacNumInfo, "FacilityName"); 
        }         private void add_button_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                FNReport = (CurrencyManager)this.BindingContext[FacNumInfo]; 
                
                FNReport.AddNew();//add new record 
                
                this.textNum.Focus();//focus on the first field 
              
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        }         private void delete_button_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                if (MessageBox.Show("Are you sure you want delete?", "Notice", MessageBoxButtons.YesNo) == DialogResult.Yes) 
                { 
                    FNReport = (CurrencyManager)this.BindingContext[FacNumInfo]; 
                    FNReport.RemoveAt(FNReport.Position); 
                    FacilityInfoAdapter.DeleteCommand.Connection = MyConn;//update to database 
                    FacilityInfoAdapter.Update(FacNumInfo); 
                    MessageBox.Show("Delete Successfully!"); 
                    return; 
                } 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("Can not delete the using data!", "Notice"); 
            } 
        }         private void submit_button_Click(object sender, EventArgs e) 
        { 
            FNReport = (CurrencyManager)this.BindingContext[FacNumInfo]; 
            if (this.textNum.Text.Trim() == "")//checking nonempty field 
            { 
                MessageBox.Show("Facility Number can not be empty!"); 
                return; 
            } 
            for (int i = 0; i < FacNumInfo.Rows.Count; i++) 
            { 
                this.numRow = FacNumInfo.Rows[i]; 
                if (numRow[0].ToString().Trim() == this.textNum.Text.Trim()) 
                { 
                    MessageBox.Show("Facility Number must be the only!"); 
                    this.FacNumInfo.RejectChanges(); 
                    return; 
                } 
            } 
            FNReport.EndCurrentEdit(); 
            if (FacNumInfo.GetChanges() != null)//whehter information is edited 
            { 
                try 
                { 
                    FacilityInfoAdapter.UpdateCommand.Connection = MyConn;//update to database 
                    FacilityInfoAdapter.InsertCommand.Connection = MyConn; 
                    FacNumTran = MyConn.BeginTransaction(); 
                    FacilityInfoAdapter.UpdateCommand.Transaction = FacNumTran; 
                    FacilityInfoAdapter.InsertCommand.Transaction = FacNumTran; 
                    this.FacilityInfoAdapter.Update(FacNumInfo); 
                    FacNumTran.Commit(); 
                    MessageBox.Show("OK"); 
                } 
                catch (Exception ex) 
                { 
                    MessageBox.Show(ex.Message);                 } 
            } 
            else 
            { 
                MessageBox.Show("You do not have to amend any information!"); 
                return; 
            }         }         private void cancel_button_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                FNReport = (CurrencyManager)this.BindingContext[FacNumInfo]; 
                FNReport.CancelCurrentEdit(); //cancel edit 
                MessageBox.Show("Have been cancelled!"); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.ToString()); 
            } 
            return; 
        }         private void dataGrid1_Select(object sender, System.EventArgs ne) 
        { 
            if (this.FacNumInfo.Rows.Count > 0) 
            { 
                int currentRow = this.dataGrid.CurrentCell.RowNumber; 
                if (currentRow >= 0 && currentRow < FNReport.Count) 
                    FNReport.Position = currentRow; 
            } 
        }  
    } 
}
谁能给我解释一个这段吗。为什么我照实做了。datagridview里面没有显示新添的数据呢。
对于currencymanager不是很懂。
对联接数据库也不熟。。
哪位帮一下忙。谢谢啊!!!!

解决方案 »

  1.   

    是datagridview
    两个不一样吗??
      

  2.   

    public partial class EquInfFrm : Form
        {
            IMyDataLinker EquLinker = new MyDataLinker();
            private DataRow equNumRow;
            private CurrencyManager FNReport;
            public EquInfFrm()
            {
                InitializeComponent();
            }        private void EquInfFrm_Load(object sender, EventArgs e)
            {
                this.EquInfGrid.DataSource = EquLinker.GetEquInfData().DefaultView;
                FNReport = (CurrencyManager)BindingContext[EquLinker.GetEquInfData()];
                EquInfGrid.Columns[0].HeaderText = "设备号";   //Datagrid里各列的名字
                EquInfGrid.Columns[1].HeaderText = "设备名称";
                DataGridTableStyle equGS = new DataGridTableStyle();
                this.EquNumTxt.DataBindings.Add("Text", EquLinker.GetEquInfData(), "EquNum");//绑定到Text
                this.EquNameTxt.DataBindings.Add("Text", EquLinker.GetEquInfData(), "EquName");
                equGS.AlternatingBackColor = Color.Blue; //颜色设置
                equGS.MappingName = EquLinker.GetEquInfData().TableName; //
                
            }        private void EquAddBtn_Click(object sender, EventArgs e)   //增加记录
            {
                try
                {
                    FNReport = (CurrencyManager)this.BindingContext[EquLinker.GetEquInfData()];
                    FNReport.AddNew();
                    this.EquNumTxt.Focus();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
           
            }
    这是我自己写的代码。EquLinker.GetEquInfData()是我自己另外写的一个方法,取得TABLE[0].
    哪位高手帮我看一下吧。
    求救!~!!!!
      

  3.   

    代码这么多懒得看了   你添加完后数据库里面是不是添加了  如果数据库有 datagridview没有 那你重新绑定数据源试下