[英]C# WPF datagrid find and delete row
Help me.帮我。 i have
textbox
for find and datagrid
for find .我有用于查找的
textbox
和用于查找的datagrid
。 I want to search in the first column the rows with data similar to the data imported from the textbox
and delete it.我想在第一列中搜索数据与从
textbox
导入的数据相似的行并将其删除。 How do I do?我该怎么办?
Thank for help.感谢您的帮助。 lucky new month
幸运的新月
My code i try:我的代码我尝试:
for (int i = 0; i < datagrid.Items.Count; i++)
{
TextBlock cellValue = datagrid.Columns[i].GetCellContent(datagrid.Items[0]) as TextBlock;
string cellValue2 = cellValue.Text;
if (cellValue2 == textbox.text) // check the search_string is present in the row of ColumnName
{
datagrid.Items.RemoveAt(i);
}
}
This code should work:此代码应该工作:
for (int i = datagrid.Items.Count-1; i >= 0; i--)
{
var cellValue = datagrid.Columns[0].GetCellContent(datagrid.Items[i]) as TextBlock;
string cellValue2 = cellValue.Text;
if (cellValue2 == textbox.Text) // check the search_string is present in the row of ColumnName
{
//For the case you set an ItemsSource:
(datagrid.ItemsSource as IList)?.Remove(datagrid.Items[i]);
//For the case you add Items directly:
try
{
datagrid.Items.Remove(datagrid.Items[i]);
}
catch (Exception)
{
}
}
}
datagrid.Items.Refresh();
Don't forget to refresh the grid: datagrid.Items.Refresh()
不要忘记刷新网格:
datagrid.Items.Refresh()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.