使用C#DataGridView更新SQL表?

使用C#DataGridView更新SQL表?,c#,sql,sql-server,datagridview,sql-server-ce,C#,Sql,Sql Server,Datagridview,Sql Server Ce,我正在一个包含DataGridView的程序中生成一个表单。有一些特定的按钮用于查看数据库中的不同表,这些按钮工作正常。但是,我还希望能够编辑表的内容并将更改发送回数据库。在按下“编辑预订”按钮时,DataGridView的数据源设置为连接到SQL语句的数据适配器: SELECT * FROM bookings 这很好用;输出显示表格,我有以下行: adminDataGrid.AllowUserToDeleteRows = true; adminDataGrid.AllowUserToAddR

我正在一个包含DataGridView的程序中生成一个表单。有一些特定的按钮用于查看数据库中的不同表,这些按钮工作正常。但是,我还希望能够编辑表的内容并将更改发送回数据库。在按下“编辑预订”按钮时,DataGridView的数据源设置为连接到SQL语句的数据适配器:

SELECT * FROM bookings
这很好用;输出显示表格,我有以下行:

adminDataGrid.AllowUserToDeleteRows = true;
adminDataGrid.AllowUserToAddRows = true;
允许在数据网格中进行编辑。我遇到的问题是更新数据网格中的任何输入。如果可能的话,我想在按下“更新按钮”或按下其他按钮时更新表,但我尝试使用SqlCommandBuilder生成insert和update命令,然后调用DataAdapter.update(),但我认为我做了一些非常错误的事情。有人能告诉我怎样才能达到我期望的目标吗

提前感谢,, Michael。

在更新按钮中:

int id; 
string name;
string address;  

for (int i = 0; i <= adminDataGrid.Rows.Count-1; i++)
{
    id = int.Parse(adminDataGrid.Rows[i].Cells["ID"].Value);
    name = adminDataGrid.Rows[i].Cells["NAME"].Value.ToString();
    address = adminDataGrid.Rows[i].Cells["Address"].Value.ToString();
// now write the C# code to update your table using id, name and address. in first cycle of the for loop the table will update with first row data of your adminDataGrid. 
}
int-id;
字符串名;
字符串地址;
对于更新按钮中的(int i=0;i:

int id; 
string name;
string address;  

for (int i = 0; i <= adminDataGrid.Rows.Count-1; i++)
{
    id = int.Parse(adminDataGrid.Rows[i].Cells["ID"].Value);
    name = adminDataGrid.Rows[i].Cells["NAME"].Value.ToString();
    address = adminDataGrid.Rows[i].Cells["Address"].Value.ToString();
// now write the C# code to update your table using id, name and address. in first cycle of the for loop the table will update with first row data of your adminDataGrid. 
}
int-id;
字符串名;
字符串地址;

对于(int i=0;我找到了这些,但我仍然不能完全确定全部内容:您可以查看我的SQL Server Compact工具箱项目中的ResultSetGrid我找到了这些,但我仍然不能完全确定全部内容:您可以查看我的SQL Server Compact工具箱项目中的ResultSetGrid试图实现这一点,但是似乎没有关键字“Text”。我尝试改为使用Value,但我认为这会返回一个对象引用。是否要使用adminDataGrid中新添加的数据更新sql表。请告诉我我是否正确?很抱歉,回复太晚。是的,这是正确的;需要将DataGrid中修改的数据写入表中。谢谢除了我需要为变量添加显式转换之外,您所做的编辑都是有效的已完成作业。已尝试实现此操作,但似乎没有关键字“Text”。我尝试改用Value,但我认为这会返回一个对象引用。是否要使用adminDataGrid中新添加的数据更新sql表。请告诉我我是否正确?很抱歉,答复太晚。是的,这是正确的;DataGrid中已修改的数据eds将写入表中。谢谢。您所做的编辑工作正常,但我需要为变量添加显式转换。只需“ToString();”即可。