Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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和SqlDataAdapter未正确更新_C#_Datagridview_Bindingsource_Sqldataadapter - Fatal编程技术网

C# DataGridView和SqlDataAdapter未正确更新

C# DataGridView和SqlDataAdapter未正确更新,c#,datagridview,bindingsource,sqldataadapter,C#,Datagridview,Bindingsource,Sqldataadapter,我一直在四处寻找,我不知道这个问题是从哪里来的。现在的情况是,我有一个使用BindingSource、SqlDataAdapter、SqlCommandBuilder和DataTable的datagridview。datagridview由一个简单的select查询填充,该查询只使用MSSQL服务器数据库中的一个表。我希望能够编辑此表中的内容。现在,编辑是有效的,但不是它应该的方式。我认为我可以编辑一个单元格,按enter键,并将更改提交给数据库。实际发生的情况是,在我完成编辑第二个单元格之前,

我一直在四处寻找,我不知道这个问题是从哪里来的。现在的情况是,我有一个使用BindingSource、SqlDataAdapter、SqlCommandBuilder和DataTable的datagridview。datagridview由一个简单的select查询填充,该查询只使用MSSQL服务器数据库中的一个表。我希望能够编辑此表中的内容。现在,编辑是有效的,但不是它应该的方式。我认为我可以编辑一个单元格,按enter键,并将更改提交给数据库。实际发生的情况是,在我完成编辑第二个单元格之前,更改不会出现。有人知道我在这里忽略了什么吗?谢谢

以下是.cs文件中的相关代码:

public partial class CheckIn : Form
{

    private BindingSource searchDGVBindingSource = new BindingSource();
    private SqlDataAdapter searchDGVDataAdapter;
    private SqlCommandBuilder searchDGVSqlCommandBuilder;
    private DataTable  searchDGVDataTable;


    public CheckIn()
    {
        InitializeComponent();
        this.Load += new System.EventHandler(CheckIn_Load);
    }

    private void CheckIn_Load(object sender, EventArgs e)
    {
        searchDGV.DataSource = searchDGVBindingSource;
        searchDGVDataAdapter = new SqlDataAdapter(selectCommand, connectionString);
        searchDGVSqlCommandBuilder = new SqlCommandBuilder(searchDGVDataAdapter);
        searchDGVDataTable = new DataTable();
        searchDGVDataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
        searchDGVDataAdapter.Fill(searchDGVDataTable);
        searchDGVBindingSource.DataSource = searchDGVDataTable;

        searchDGV.AutoResizeColumns();
    }

    private void searchGridView_RowEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        searchDGVDataAdapter.Update(searchDGVDataTable);
    }
}
以下是.Designer.cs文件中的相关代码:

public partial class CheckIn : Form
{

    private BindingSource searchDGVBindingSource = new BindingSource();
    private SqlDataAdapter searchDGVDataAdapter;
    private SqlCommandBuilder searchDGVSqlCommandBuilder;
    private DataTable  searchDGVDataTable;


    public CheckIn()
    {
        InitializeComponent();
        this.Load += new System.EventHandler(CheckIn_Load);
    }

    private void CheckIn_Load(object sender, EventArgs e)
    {
        searchDGV.DataSource = searchDGVBindingSource;
        searchDGVDataAdapter = new SqlDataAdapter(selectCommand, connectionString);
        searchDGVSqlCommandBuilder = new SqlCommandBuilder(searchDGVDataAdapter);
        searchDGVDataTable = new DataTable();
        searchDGVDataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
        searchDGVDataAdapter.Fill(searchDGVDataTable);
        searchDGVBindingSource.DataSource = searchDGVDataTable;

        searchDGV.AutoResizeColumns();
    }

    private void searchGridView_RowEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        searchDGVDataAdapter.Update(searchDGVDataTable);
    }
}
        // 
        // searchDGV
        // 
        this.searchDGV.AllowUserToAddRows = false;
        this.searchDGV.AllowUserToDeleteRows = false;
        this.searchDGV.BackgroundColor = System.Drawing.Color.White;
        this.searchDGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.searchDGV.Location = new System.Drawing.Point(10, 250);
        this.searchDGV.MultiSelect = false;
        this.searchDGV.Name = "searchDGV";
        this.searchDGV.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.searchDGV.Size = new System.Drawing.Size(619, 150);
        this.searchDGV.TabIndex = 7;
        this.searchDGV.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.searchGridView_CellClick);
        this.searchDGV.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.searchGridView_RowEndEdit);
您需要在事件处理程序中调用searchDGVBindingSource.EndEdit,它将起作用:

private void searchGridView_RowEndEdit(object sender, DataGridViewCellEventArgs e)
{
    searchDGVBindingSource.EndEdit();
    searchDGVDataAdapter.Update(searchDGVDataTable);
}

检查BindingSource.EndEdit以了解更多信息。

您有没有理由让名为searchGridView\u RowEndEdit的方法处理CellEndEdit事件?我只是没有将searchGridView\u RowEndEdit方法重命名为更合适的方法。我之所以这样命名是因为我尝试了另一种方法。您是否验证了您正在执行的更改实际上是有效的?如果您在事件处理程序中放置断点,它是否会在您离开第一个单元格时停止?有效更改是否指对数据库有效?如果是,则是有效的。在本例中,我所做的只是更改一个名称-只是添加一个额外的字母,作为一个简单的测试。这就成功了!我花了相当多的时间试图找到这个,谢谢!