我知道有很多关于使用SQL adapter从 a 更新数据库中的多行的信息,Datagrid但是在尝试了许多不同的方法后我似乎找不到解决方案。
我有一个SelectionChange事件填充 acomboxBox 中选择的当前行DataGrid,当dropdown它展开时它显示剩余的项目。
我还有一个button_click事件,我试图让用户从中选择所有必要的行,DataGrid然后单击 abutton以使用SelectedValue来自Combobox
这是我的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);
}
}