Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么datagridview会更新,而db不会更新?_C# - Fatal编程技术网

C# 为什么datagridview会更新,而db不会更新?

C# 为什么datagridview会更新,而db不会更新?,c#,C#,我已经搜索这个答案三天了,到目前为止,我没有找到有效的答案。我使用的是带有数据集、绑定源、表适配器、表适配器管理器、绑定导航器和DataGridView的SQL db。我试图使用左边的字段来更新数据库,并且只使用DataGridView作为视图。应用程序从文本框更新到DataGridView很好,但当我使用更新按钮调用: this.Validate(); this.directoryBindingSource.EndEdit(); this.tableAdapter

我已经搜索这个答案三天了,到目前为止,我没有找到有效的答案。我使用的是带有数据集、绑定源、表适配器、表适配器管理器、绑定导航器和DataGridView的SQL db。我试图使用左边的字段来更新数据库,并且只使用DataGridView作为视图。应用程序从文本框更新到DataGridView很好,但当我使用更新按钮调用:

this.Validate();
this.directoryBindingSource.EndEdit();               
this.tableAdapterManager.UpdateAll(this.companyContactsDataSet1);
没有发生任何事情,数据库保持不变。我不知道如何观察数据集中的变量,看看它们的值是什么,所以我不知道数据集中是否正在将值传递给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);
        }

    }
这就是我现在能想到的