How to filter two columns in c# using a textbox

ASOoOMA kayal 0 Reputation points
2023-03-29T20:11:27.0333333+00:00

I want filter two columns (1,3) using a text box. Another thing that you might need to know is that I’m using the excel file path and not a database.

private void button1_Click(object sender, EventArgs e)
{
	using(XLWorkbook workBook = new XLWorkbook(@"C:\Users\asma1\Desktop\trainees  .xlsx"))
	{
		//Read the first Sheet from Excel file.
		IXLWorksheet workSheet = workBook.Worksheet(1);
		//Create a new DataTable.
		DataTable dt = new DataTable();
		//Loop through the Worksheet rows.
		bool firstRow = true;
		foreach(IXLRow row in workSheet.Rows())
		{
			//Use the first row to add columns to DataTable.
			if(firstRow)
			{
				foreach(IXLCell cell in row.Cells())
				{
					dt.Columns.Add(cell.Value.ToString());
				}
				firstRow = false;
			}
			else
			{
				//Add rows to DataTable.
				dt.Rows.Add();
				int i = 0;
				foreach(IXLCell cell in row.Cells())
				{
					dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
					i++;
				}
				workBook.SaveAs(@"C:\Users\asma1\Desktop\   trainees.xlsx");
			}
			dataGridView1.DataSource = dt;
		}
	}
}
private void search2_Click(object sender, EventArgs e)
{
	string searchValue = textBox2.Text;
	string folder_name = "";
	int rowIndex = -1;
	dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
	try
	{
		bool valueResult = false;
		dataGridView1.ClearSelection();
		foreach(DataGridViewRow row in dataGridView1.Rows)
		{
			if((row.Cells[3].Value != null && row.Cells[3].Value.ToString().Equals(searchValue)) || (row.Cells[1].Value != null && row.Cells[1].Value.ToString().Equals(searchValue)))
			{
				rowIndex = row.Index;
				dataGridView1.ClearSelection();
				row.Selected = true;
				valueResult = true;
				dataGridView1.FirstDisplayedScrollingRowIndex = rowIndex;
				dataGridView1.Focus();
				folder_name = row.Cells[1].Value.ToString();
				try
				{
					string currentDirectoryPath = root_path + "/" + folder_name;
					if(Directory.Exists(currentDirectoryPath))
					{
						images_path = Directory.GetFiles(currentDirectoryPath, "*.jpg");
						show_image(image_counter);
					}
					else
					{
						if(pictureBox1.Image != null)
						{
							pictureBox1.Image.Dispose();
							pictureBox1.Image = null;
							images_path = new string[]
							{};
							image_counter = 0;
						}
					}
				}
				catch
				{}
				break;
			}
		}
		if(!valueResult)
		{
			MessageBox.Show("Unable to find " + textBox2.Text, "Not Found");
			if(pictureBox1.Image != null)
			{
				pictureBox1.Image.Dispose();
				pictureBox1.Image = null;
				images_path = new string[]
				{};
				image_counter = 0;
			}
			return;
		}
	}
	catch(Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
}
Microsoft 365 and Office Excel For business Windows
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Anonymous
    2023-04-04T09:32:25.98+00:00

    Hi @ASOoOMA kayal, Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

    if ((row.Cells[0].Value != null && row.Cells[0].Value.ToString().Equals(searchValue)) || (row.Cells[2].Value != null && row.Cells[2].Value.ToString().Equals(searchValue)))
    

    User's image

    You didn't show your show_image code.

    From what I've read, you might be able to tell there's something wrong here.

    In datagrideview.row.Cells start from 0. The 1 and 3 you searched for should be 0 and 2 here.

    If you have anything to add, please comment below and I will continue to follow up.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.