Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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/5/sql/74.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中上载修改后的数据?_C#_Sql_Datagridview - Fatal编程技术网

C# 如何在Datagridview中上载修改后的数据?

C# 如何在Datagridview中上载修改后的数据?,c#,sql,datagridview,C#,Sql,Datagridview,我已经从SQL数据库中提取了一些数据到datagridview中,但是在用户修改了datagridview中的数据之后,我如何才能将数据上传回来 我还发现了这样的代码: this.dataGridView1.Update(); 这个方法更新了什么?下面是我将数据绑定到datagridview的代码: SqlDataReader read; SqlCommand cmd; DataTable dt = new DataTable(); cmd = new SqlCommand("Select *

我已经从SQL数据库中提取了一些数据到datagridview中,但是在用户修改了datagridview中的数据之后,我如何才能将数据上传回来

我还发现了这样的代码:

this.dataGridView1.Update();
这个方法更新了什么?下面是我将数据绑定到datagridview的代码:

SqlDataReader read;
SqlCommand cmd;
DataTable dt = new DataTable();
cmd = new SqlCommand("Select * from Table", 204.192.49.3);
read = cmd.ExecuteReader();
dt.Load(read);
dataGridView1.DataSource = dt;

为此,必须使用Datasource属性和SqlDataAdapter填充datagrid。您只需更新adadpter对象。

您不能使用此“this.dataGridView1.update();”代码更新您的网格
在db中更新数据后,必须将网格视图绑定到db。最简单的方法是创建一个临时数据集,使用SqlDataAdapter填充数据,然后将其作为dataGridView的数据源传递。这段代码应该做到以下几点:
private void dataGridVies1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
        SqlCommandBuilder cb = new SqlCommandBuilder(sda);
        sda.Update(ds);
}

public partial class Form2 : Form
{

       private void Form2_Load(object sender, EventArgs e)
    {
        con.ConnectionString = connectionstr;

        sda = new SqlDataAdapter("select * from table_user", con);//define dataadapter
        sda.Fill(ds );
        dataGridView1.DataSource = ds.Tables[0];//bind the data,now the datagridview can show up the data

    }
    string connectionstr = "integrated security=SSPI;Initial Catalog=****;Data Source=localhost;";//init

    SqlConnection con = new SqlConnection();//
    SqlDataAdapter sda = new SqlDataAdapter();//

    DataSet ds = new DataSet();
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        //you can not update sda here

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    private void dataGridVies1_CellValidated(object sender, DataGridViewCellEventArgs e)
    {
        SqlCommandBuilder cb = new SqlCommandBuilder(sda);//auto generate the cmd of create, delete and update 
        sda.Update(ds);//flush to database
     }


}
DataSet temp=新数据集();
SqlDataAdapter SDA=新的SqlDataAdapter();
SqlCommand=newsqlcommand();
SqlConnection=newsqlconnection();
string connstring=“YourConnectionString”

然后在要触发更新的方法中执行以下操作:

`connection.Open();
 command.CommandText = "SELECT * FROM Table" //Adjust the SQL query to your needs
 command.Connection = connection;
 SDA.SelectCommand = command;
 SDA.Fill(temp);
 dataGridView1.DataSource = temp.Tables[0].DefaultView;`

这应该可以解决您的问题。

如何将数据加载到datagridview?你能用你的代码更新这个问题吗?我怎样才能使网格视图盲到db?我在一开始从数据库获取数据时已经这样做了,当我将数据上传回数据库时是否需要再次盲到数据库在你将数据更新到数据库后,你必须再次将网格视图绑定到db“dataGridView1.DataSource=dt;dataGridView1.Update();“此代码正确吗?dataGridView1.DataSource=updatedquery,dataGridView1.Update()在我使用代码”SqlCommandBuilder cb=new SqlCommandBuilder(sda)时不是必需的//自动生成创建、删除和更新sda.update(ds)的命令//“刷新到数据库”,我发现“DataAdapter.SelectCommand属性需要初始化”异常,这是什么?请尝试添加
sda.DeleteCommand=cb.GetDeleteCommand();sda.UpdateCommand=cb.GetUpdateCommand();sda.InsertCommand=cb.GetInsertCommand()
在更新适配器之前。我仍然得到了异常情况..在修改代码后,代码看起来像:SqlCommandBuilder cb=new SqlCommandBuilder(da);//自动生成创建、删除和更新da.DeleteCommand=cb.GetDeleteCommand();da.UpdateCommand=cb.GetUpdateCommand();da.InsertCommand=cb.GetInsertCommand();da.Update(dt);//刷新到数据库我想要的是在修改数据后将数据上传回sql server,但在我运行代码后,它似乎会获取sql server并将原始数据盲回datagrid视图……如何修改数据。您是直接在dataGridView中进行修改,还是使用sql查询?我需要修改DataGridView中的引用直接在这种情况下,您可以向DataGridView中添加一个按钮列,该列将触发SQL更新命令。当您输入所需的所有修改并单击该按钮时,创建一个构造SQL命令的方法。该方法将如下所示
Update table_name SET column1='”dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()+“…”…
的“e”是DataGridViewCellEventArg,因此最好将此代码放入DataGridView的OnCellClick或OnCellContentClick方法中。只有当单元格[e.ColumnIndex]等于按钮的索引。您还可以将数据传输到文本框中并在其中进行修改,还可以使用另一个触发SQL命令的按钮。在这种情况下,您可以使用以下内容:
textBox1.Text=dataGridView1.Rows[e.RowIndex]。单元格[0]。Value.ToString();
为dataGridView的每一列创建一个文本框,并将单个单元格内容传递给每个文本框。当您进行所需修改时,单击包含SQL命令的按钮-这次将是
Update teble_name SET column1=“+textBox1.Text+”…