[英]Filter GridView using a textbox with commas C#
I'm trying to filter trough by gridview using a textbox when I need to use commas to search for specific items in that grid view当我需要使用逗号搜索该网格视图中的特定项目时,我正在尝试使用文本框通过 gridview 过滤槽
This is my gridview:这是我的网格视图:
As you can see the column Ingredientes
contains items separated by commas.如您所见, Ingredientes
列包含用逗号分隔的项目。 What I'm trying to do is if I write in the Ingredientes
textbox "repolho,salsa" for example the row appears.我想要做的是,如果我在Ingredientes
文本框中写下“repolho,salsa”,例如出现该行。
At the moment I can search if the Ingredientes
column has just a single item like that one with the "ing"目前,我可以搜索Ingredientes
列是否只有一个类似“ing”的项目
This is my code这是我的代码
// Procura pelos ingredientes
else if (txtTitulo.Text == "" && txtIngredientes.Text != "")
{
dataGridReceitas.Rows.Clear();
if (File.Exists(receitas))
{
sr = File.OpenText(receitas);
string linha = "";
int x = 0;
while((linha = sr.ReadLine()) != null)
{
string[] campos = linha.Split(';');
if(txtIngredientes.Text == campos[3])
{
dataGridReceitas.Rows.Add(1);
dataGridReceitas[0, x].Value = campos[0];
dataGridReceitas[1, x].Value = campos[1];
dataGridReceitas[2, x].Value = campos[2];
dataGridReceitas[3, x].Value = campos[3];
x++;
}
}
sr.Close();
}
}
You can simply do this :你可以简单地这样做:
else if (txtTitulo.Text == "" && txtIngredientes.Text != "")
{
string searchValue = txtIngredientes.Text;
dataGridReceitas.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
bool valueResult = false;
foreach (DataGridViewRow row in dataGridReceitas.Rows)
{
if (row.Cells[3].Value != null && row.Cells[3].Value.ToString().Equals(searchValue))
{
int rowIndex = row.Index;
dataGridView1.Rows[rowIndex].Selected = true;
valueResult = true;
break;
}
}
if (!valueResult)
{
MessageBox.Show("Unable to find " + searchtextBox.Text, "Not Found");
return;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.