【发布时间】:2015-05-05 19:18:49
【问题描述】:
我是 C# 开发的新手。在编写应用程序时,我正在学习和挣扎——最好的学习方法是:)
我希望有人能够帮助我解决我的问题。
我正在通过 dataGrid1.ItemsSource = dt.DefaultView; 填充 WPF DataGrid 然后让 DataGrid 自动为我生成列。
用户可以单击 DataGrid 中的任何行,然后将使用该行的数据填充我在 WPF UI 上的标题部分 - 这允许用户通过标题编辑该行。我不希望他们通过 DataGrid 进行编辑。
用户可以通过标题字段编辑行,然后单击更新按钮。 UPDATE 按钮将运行一个存储过程来处理记录的所有验证和更新。保存记录后,我会启动网格刷新方法。
刷新网格后,我需要能够搜索 DataGrid 上的特定列,以便选择、设置焦点并滚动到刚刚更新的行。
我在 Google 上疯狂搜索,但在 DataGrid 上找不到如何执行此操作。有一些关于如何在 DataGridView 上执行此操作的示例,这不是我正在使用的。
非常感谢任何帮助....谢谢
这是我的按钮点击代码
private void btn_Update_Click(object sender, RoutedEventArgs e) {
// variables
bool isOkToUpdate = true;
this.Cursor = Cursors.Wait;
// Validations of certain fields
if (txt_FinanceEmpName.Text.Length > 25 )
{
MessageBox.Show("The Finance Emp Name must not exceed 25 characters in length. "
+ txt_FinanceEmpName.Text.Length.ToString()
, "Maximum characters exceeded"
, MessageBoxButton.OK, MessageBoxImage.Stop);
isOkToUpdate = false;
}
//
if (isOkToUpdate == true)
{
// create an instance
DatabaseClass objDatabaseClass = new DatabaseClass(_connectionString);
// if we are able to open and close the SQL Connection then proceed
if (objDatabaseClass.CheckSQLConnection())
{
try
{
// create instance of SqlConnection class. variable 'con' will hold the instance
SqlConnection con = new SqlConnection(_connectionString);
con.Open();
// use a using for all disposable objects, so that you are sure that they are disposed properly
using (SqlCommand cmd = new SqlCommand("usp_mktdata_update_cm_mktdata_emp_name_fits_to_finance", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@IdUnique", SqlDbType.Int).Value = Convert.ToInt32(txt_IdUnique.Text);
cmd.Parameters.Add("@FinanceEmpName", SqlDbType.VarChar).Value = txt_FinanceEmpName.Text;
cmd.Parameters.Add("@ADWindowsLogon", SqlDbType.VarChar).Value = _windowsUserName;
cmd.Parameters.Add("@ADWindowsLogonEMail", SqlDbType.VarChar).Value = _windowsUserNameEMail;
cmd.Parameters.Add("@IndActive", SqlDbType.TinyInt).Value = 1;
cmd.Parameters.Add("@RecordVersion", SqlDbType.Int).Value = Convert.ToInt32(txt_RecordVersion.Text);
//cmd.Parameters.Add("@IdJob", SqlDbType.Int).Value = "DEFAULT";
//cmd.Parameters.Add("@IdUser", SqlDbType.Int).Value = "DEFAULT";
//cmd.Parameters.Add("@outIdUnique", SqlDbType.Int).Value = "DEFAULT";
//cmd.Parameters.Add("@outRecordVersion", SqlDbType.Int).Value = "DEFAULT";
//cmd.Parameters.Add("@outInd", SqlDbType.Int).Value = "DEFAULT";
//cmd.Parameters.Add("@outMessage", SqlDbType.VarChar).Value = "DEFAULT";
cmd.ExecuteNonQuery();
}
// Last Updated Record
txt_LastUpdated_IdUnique.Text = txt_IdUnique.Text;
txt_LastUpdated_FitsEmpName.Text = txt_FitsEmpName.Text;
txt_LastUpdated_FinanceEmpName.Text = txt_FinanceEmpName.Text;
// Refresh the Datagrid - the DataGrid_MonikerName
// pass in null for the params in order to fire the event
btn_RefreshGrid_Click(null, null);
// ****************************************
// TODO: After Grid Refresh Search the column ID UNIQUE for the value stored in txt_IdUnique.Text which
// was just saved via the stored procedure
// Once the value is found in the DataGrid need to grab the DataGrid Row INdex in order to
// Select the ROW and Set the ROW to the SELECTED ROW and set focus to the DataGrid also will need to Scroll the Grid into View.
//
// ****************************************
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("Connection not established to the SQL Server. " + Environment.NewLine + "The SQL Server may be offline or valid credentials are not yet granted.", "SQL Server Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
this.Close();
}
}
this.Cursor = Cursors.Arrow;
}
【问题讨论】:
-
刷新后还是刷新前需要焦点来表示记录已经更新??
-
您好 - 感谢您的快速响应...在网格刷新后我需要它。当网格刷新时,我正在使用新记录集重新填充网格。我刚刚添加了按钮的代码单击 TODO 部分详细说明了刷新后我需要发生的事情