为什么datagridview会更新,但db不会更新?

时间:2014-08-25 00:16:37

标签: c#

我一直在搜索这个答案三天,到目前为止我找不到答案。我正在使用带有DataSet,绑定源,表适配器,表适配器管理器,绑定导航器和DataGridView的SQL数据库。我试图使用左侧的字段来更新数据库,并使用DataGridView作为视图。应用程序从文本框更新到DataGridView就好了但是当我使用Update按钮调用时:

this.Validate();
this.directoryBindingSource.EndEdit();               
this.tableAdapterManager.UpdateAll(this.companyContactsDataSet1);

没有任何反应,数据库保持不变。我不知道如何在数据集中观察变量,看看它们的值是什么,所以我不知道DataSet是否将值传递给UpdateAll函数。

这是主要代码:

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'companyContactsDataSet1.Directory' table. You can move, or remove it, as needed.
        this.directoryTableAdapter.Fill(this.companyContactsDataSet1.Directory);

        lblTotalRows.Text = "Total number of records: " +directoryBindingSource.Count.ToString();
        position = directoryBindingSource.Position + 1;
        lblCurrentRow.Text = "Current Row: " + position.ToString();
    }

    private void bindingNavigator1_RefreshItems(object sender, EventArgs e)
    {
        //lblCurrentRowCopy.Text = "Current Row: "; //+ this.bindingNavigator1.CountItemFormat.ToString();            
    }

    int position = 0;
    private void btnPrevious_Click(object sender, EventArgs e)
    {
        directoryBindingSource.MovePrevious();
        position = directoryBindingSource.Position + 1;
        lblCurrentRow.Text = "Current Row: " + position.ToString();
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        directoryBindingSource.MoveNext();
        position = directoryBindingSource.Position + 1;
        lblCurrentRow.Text = "Current Row: " + position.ToString();
    }

    int count = 0;
    private void btnAddRecord_Click(object sender, EventArgs e)
    {
        //This event handler moves to the last record and gets the value of the ID column.
        //Then it parces that string value in the count variable. The position of the records
        //is recorded and sent to the position label and a new row is added for the next record.
        //The add.New function clears the fields and begins a new row. Then the count variable 
        //is incremented by one and cast to string to be placed into the idTextBox field.
        //This was my solution to not having a way to use an autonumber field for ID. When I 
        //tried to use the SQL newID function in a table it was not allowed. I had many problems trying to use this line
        //[Id] uniqueidentifier ROWGUIDCOL DEFAULT NEWID() not null IDENTITY ,

        directoryBindingSource.MoveLast();
        count = Int32.Parse(this.dataGridView1.CurrentCell.Value.ToString());

        directoryBindingSource.AddNew(); // This throws an exception if the cursor is in any column but the ID column when caoo

        count++;
        idTextBox.Text = count.ToString();
        position = directoryBindingSource.Position + 1;
        lblCurrentRow.Text = "Current Row: " + position.ToString();
        lblTotalRows.Text = "Total number of records: " + directoryBindingSource.Count.ToString();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        //need to call binding source update method

        try
        {                
            //this.directoryTableAdapter.Insert(count, fNameTextBox.Text, lNameTextBox.Text, addressTextBox.Text, cityTextBox.Text,
                //stateTextBox.Text, zipTextBox.Text, emailTextBox.Text, phoneTextBox.Text, deptTextBox.Text);

            this.Validate();
            this.directoryBindingSource.EndEdit();

            this.tableAdapterManager.UpdateAll(this.companyContactsDataSet1);

            //this.companyContactsDataSet1.AcceptChanges();          
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

这就是我现在所能想到的。

0 个答案:

没有答案