【发布时间】:2020-09-07 15:30:43
【问题描述】:
我知道有很多关于使用SQL adapter 从Datagrid 更新数据库中的多行的信息,但是在尝试了许多不同的方法后我似乎找不到解决方案。
我有一个SelectionChange 事件,它使用在DataGrid 中选择的当前行填充comboxBox,当dropdown 展开时,它会显示剩余的项目。
我还有一个button_click 事件,我试图让用户从DataGrid 中选择所有必要的行,然后单击button 以使用Combobox 中的SelectedValue 更新这些行
这是我的ButtonClick 事件,我已经能够更新选定的行但无法实现更新多行。如果需要,我可以提供我的SelectionChange 事件和DataGrid 的代码:
private void butn_Assign_Click(object sender, RoutedEventArgs e)
{
try
{
SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
connection.Open();
int x;
// Set the UPDATE command and parameters.
sAdapter.UpdateCommand = new SqlCommand("UPDATE [hb_Disputes] SET ASSGNTO=@ASSGNTO WHERE DSP_ID=@DSP_ID;", connection);
sAdapter.UpdateCommand.Parameters.Add("@DSP_ID", SqlDbType.Int, 500).Value = txt_ID.Text;
sAdapter.UpdateCommand.Parameters.Add("@ASSGNTO", SqlDbType.NVarChar, 10).Value = cmb_AnalystName.SelectedValue;
sAdapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.Both;
// Execute the update.
x = sAdapter.UpdateCommand.ExecuteNonQuery();
if (x >= 1)
{
MessageBox.Show("Dispute has been assigned");
}
connection.Close();
//Update User's High Bill Dispute Count
Window parentWindow = Window.GetWindow(this);
((MainWindow)parentWindow).HBD_Count();
// Clear Search Fields
cmb_AnalystName.SelectedIndex = -1;
//Refresh DataGrid
AssignList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
【问题讨论】:
标签: c# sql-server wpf datagrid updatecommand