将 gridview 更新数据保存到 SQL 服务器?
Saving gridview updated data to SQL Server?
我可以在我的 gridView 中保存数据,但我无法对我的数据源这样做。
是不是少了一行代码,或者是我遗漏了什么?
这是我的代码:
public Marksheet(object val1)
{
InitializeComponent();
string connectionString = null;
SqlConnection conn;
connectionString = "Server=localhost\SQLEXPRESS;Integrated security=SSPI;database=jms";
SqlDataAdapter sda6 = new SqlDataAdapter("SELECT * FROM grades WHERE class_code='" + val1 + "'", connectionString);
conn = new SqlConnection(connectionString);
DataTable dt5 = new System.Data.DataTable();
sda6.Fill(dt5);
gridControl1.DataSource = dt5;
}
private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
{
if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.EndEdit)
{
if (MessageBox.Show("Do you want to commit changes to the current record?", "Confirm commit",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.No)
{
gridView1.CloseEditor();
gridView1.UpdateCurrentRow();
}
}
}
private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
//?? Could there be something I'm missing here? if yes, what could it be?
}
当您对网格控件进行任何更改时,更改会反映在数据源中,在您的情况下是 DataTable
。如果您从逻辑上思考,这似乎是正确的,因为网格控件绑定到 DataTable
并且不知道 DataTable
是如何填充的。
现在您可以看到 DataTable
是使用 DataAdapter
填充的。您需要调用 DataAdapter.Update(dataTable)
方法将更改推送到数据库。
@Aseem 已经建议最好的方法是您需要实施 ADO.net 绑定以使用 DataAdapter
[=29= 将更改提交回后端].如果您可以通过这种方式实现,请查看以下教程:
When binding to a database using ADO.NET, you bind a control to a
DataTable containing data from the database. When you change data via
the Grid Control (adding, deleting or modifying records), the changes
are accumulated in the DataTable. They are not automatically posted to
the underlying database. Therefore, you need to manually call a
specific method to post the changes.
如果您正在使用自定义数据 table 并且不愿意实施此类绑定,那么您必须处理 GridView.RowUpdated Event 然后您可以 post 返回您在当前所做的更改更新行。
参考这个:Xtragrid Row Updated Event
示例:
Private Sub gridView1_RowUpdated(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowObjectEventArgs) Handles gridView1.RowUpdated
Dim val As Object
Dim row As DataRowView = CType(e.Row, DataRowView)
val = row(0)
End Sub
希望对您有所帮助..
我可以在我的 gridView 中保存数据,但我无法对我的数据源这样做。 是不是少了一行代码,或者是我遗漏了什么?
这是我的代码:
public Marksheet(object val1)
{
InitializeComponent();
string connectionString = null;
SqlConnection conn;
connectionString = "Server=localhost\SQLEXPRESS;Integrated security=SSPI;database=jms";
SqlDataAdapter sda6 = new SqlDataAdapter("SELECT * FROM grades WHERE class_code='" + val1 + "'", connectionString);
conn = new SqlConnection(connectionString);
DataTable dt5 = new System.Data.DataTable();
sda6.Fill(dt5);
gridControl1.DataSource = dt5;
}
private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
{
if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.EndEdit)
{
if (MessageBox.Show("Do you want to commit changes to the current record?", "Confirm commit",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.No)
{
gridView1.CloseEditor();
gridView1.UpdateCurrentRow();
}
}
}
private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
//?? Could there be something I'm missing here? if yes, what could it be?
}
当您对网格控件进行任何更改时,更改会反映在数据源中,在您的情况下是 DataTable
。如果您从逻辑上思考,这似乎是正确的,因为网格控件绑定到 DataTable
并且不知道 DataTable
是如何填充的。
现在您可以看到 DataTable
是使用 DataAdapter
填充的。您需要调用 DataAdapter.Update(dataTable)
方法将更改推送到数据库。
@Aseem 已经建议最好的方法是您需要实施 ADO.net 绑定以使用 DataAdapter
[=29= 将更改提交回后端].如果您可以通过这种方式实现,请查看以下教程:
When binding to a database using ADO.NET, you bind a control to a DataTable containing data from the database. When you change data via the Grid Control (adding, deleting or modifying records), the changes are accumulated in the DataTable. They are not automatically posted to the underlying database. Therefore, you need to manually call a specific method to post the changes.
如果您正在使用自定义数据 table 并且不愿意实施此类绑定,那么您必须处理 GridView.RowUpdated Event 然后您可以 post 返回您在当前所做的更改更新行。
参考这个:Xtragrid Row Updated Event
示例:
Private Sub gridView1_RowUpdated(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowObjectEventArgs) Handles gridView1.RowUpdated
Dim val As Object
Dim row As DataRowView = CType(e.Row, DataRowView)
val = row(0)
End Sub
希望对您有所帮助..