C# C WPF DataGrid在列中搜索值,返回行索引
我是C开发的新手。在编写应用程序的过程中,我在学习和奋斗——学习的最佳方式是: 我希望有人能帮助我解决这个问题 我通过dataGrid1.ItemsSource=dt.DefaultView填充WPF DataGrid; 然后让DataGrid自动为我生成列 用户可以单击DataGrid中的任何行,然后用行的数据填充WPF UI上的标题部分-这允许用户通过标题编辑行。我不希望他们通过DataGrid进行编辑 用户可以通过标题字段编辑行,然后单击更新按钮。UPDATE按钮将运行一个存储过程,该过程处理记录的所有验证和更新。一旦记录被保存,我就会启动网格刷新方法 刷新网格后,我需要能够搜索DataGrid上的特定列,以便选择、设置焦点并滚动到刚刚更新的行 我在谷歌上疯狂地搜索,但我就是找不到如何在DataGrid上做到这一点。这里有一些关于如何在DataGridView上执行此操作的示例,这不是我正在使用的 非常感谢您的帮助……谢谢 这是我点击按钮的代码 私有无效btn\u更新\u单击对象发送者,路由目标e {C# C WPF DataGrid在列中搜索值,返回行索引,c#,wpf,datagrid,C#,Wpf,Datagrid,我是C开发的新手。在编写应用程序的过程中,我在学习和奋斗——学习的最佳方式是: 我希望有人能帮助我解决这个问题 我通过dataGrid1.ItemsSource=dt.DefaultView填充WPF DataGrid; 然后让DataGrid自动为我生成列 用户可以单击DataGrid中的任何行,然后用行的数据填充WPF UI上的标题部分-这允许用户通过标题编辑行。我不希望他们通过DataGrid进行编辑 用户可以通过标题字段编辑行,然后单击更新按钮。UPDATE按钮将运行一个存储过程,该过程
// 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;
}
我也为我们的系统中的网格使用DataTable.DefaultView。我还为MyDataGrid对DataGrid进行了子分类…在MyDataGrid上,我有一个自定义方法,可以根据主键强制加载给定的ID。因此,我在网格上有另一个自定义属性,主键列的名称是什么,因为它应该在e数据视图,即使它可能无法在网格中直观显示 根据您提供的内容,这里是从我的原始代码到您的特定环境的修改
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();
我将数据网格作为选定值的整行,而不是每个单元格。因此,您可能需要稍微处理对象引用…一旦找到该行,您还可以尝试使用dataGrid1.CurrentItem
希望这能让你更接近你的最终决定
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;
}
}
}
在网格刷新调用-TryGridRefreshtxt\u LastUpdated\u IdUnique.Text、dataGrid\u MonikerName、gridData\u ColumnToSearch之后,该方法会像这样被调用
我还得换衣服
datagrid DataRowView从CurrentCell.Item选择EdItem,以捕获选定值的整行,而不是每个单元格
//DataRowView _DataView=dataGrid_MonikerName.CurrentCell.Item作为DataRowView;
DataRowView\u DataView=dataGrid\u MonikerName.SelectedItem as DataRowView;刷新后还是刷新前需要焦点来指示记录已更新??您好-感谢您的快速响应…我在网格刷新后需要它。当网格刷新时,我正在用新记录集重新填充网格。我刚刚添加了代码f或者单击按钮“待办事项”部分详细说明刷新后需要执行的操作感谢您的响应…我正在查看您的响应和代码片段,以了解如何将其合并到我需要执行的操作中。将很快更新我在尝试合并您的代码片段时遇到困难:。请查看e代码,我已经包括,看看是否片段可以合并在那里。thks@RJP,修改后的答案…尝试一下并让我知道。非常感谢您的修改…我正在尝试修改。希望很快会更新。非常感谢您…您的最终修改解决了我的问题。我花了一个多星期的时间搜索谷歌…每次我一直来到StackOverFlow.com,它只在创建帐户和发布问题时才有意义。是的,这个社区帮助我。我今晚可以睡觉了。非常感谢。我将更新帖子,以显示您修改后的解决方案和我的调整…以帮助其他人。谢谢