我是C#开发的新手。我正在学习和努力,因为我正在编写一个应用程序 - 最好的学习方法是:)
我希望有人能够帮助我处理我的问题。
我通过 dataGrid1.ItemsSource = dt.DefaultView填充 WPF DataGrid ; 然后让DataGrid自动为我生成列。
用户可以单击DataGrid中的任何行,然后将使用行的数据填充WPF UI上的标题部分 - 这允许用户通过标题编辑行。我不希望他们通过DataGrid进行编辑。
用户可以通过标题字段编辑行,然后单击UPDATE按钮。 UPDATE按钮将运行存储过程,该过程处理所有验证和更新记录。保存记录后,我将关闭网格刷新方法。
刷新网格后,我需要能够搜索DataGrid上的特定列以便选择,设置Focus并滚动到刚刚更新的行。
我在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;
}
答案 0 :(得分:2)
我也使用DataTable.DefaultView处理系统中的网格。我还将DataGrid分为“MyDataGrid”...在MyDataGrid上,我有一个自定义方法来强制基于其主键加载给定的ID。所以我在网格上有另一个自定义属性,因为它应该在数据视图中找到主键列的名称,即使它可能没有在网格中直观显示。
根据您提供的内容,以下是我原始代码对您特定环境的修改......
public void TryGridRefresh()
{
int IDToFind = Convert.ToInt32(txt_IdUnique.Text);
if (IDToFind > -1 && dataGrid1.ItemsSource is DataView )
{
foreach( DataRowView drv in (DataView)dataGrid1.ItemsSource )
if ((int)drv["IdUnique"] == IDToFind)
{
// This is the data row view record you want...
dataGrid1.SelectedItem = drv;
}
}
}
所以,在你致电
之后 btn_RefreshGrid_Click(null, null);
然后致电
TryGridRefresh();
我将数据网格作为所选值的整个ROW而不是每个单元格。所以你可能需要稍微使用对象引用...一旦找到行,你也可以尝试使用dataGrid1.CurrentItem ......
希望这会让你更接近你的最终解决方案。
答案 1 :(得分:0)
public void TryGridRefresh(string IdUniqueToFind, DataGrid MyDataGrid, string MyGridColumnToSearch)
{
int IDToFind = Convert.ToInt32(IdUniqueToFind);
// this if did not work for me..got errors
//if (IDToFind > -1) and (dataGrid_MonikerName.ItemsSource is DataView )
//{
foreach (DataRowView drv in (DataView)MyDataGrid.ItemsSource)
{
if ((int)drv[MyGridColumnToSearch] == IDToFind)
{
// This is the data row view record you want...
MyDataGrid.SelectedItem = drv;
MyDataGrid.ScrollIntoView(drv);
MyDataGrid.Focus();
break;
}
}
}
在网格刷新调用之后调用Method,如下所示: TryGridRefresh(txt_LastUpdated_IdUnique.Text,dataGrid_MonikerName,gridData_ColumnToSearch);
我也不得不改变 datagrid DataRowView从CurrentCell.Item到SelectedItem,以便捕获所选值的整行而不是每个单元格。
// DataRowView _DataView = dataGrid_MonikerName.CurrentCell.Item as DataRowView; DataRowView _DataView = dataGrid_MonikerName.SelectedItem as DataRowView;