Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PICTURE BELOW.

Data Loaded
public void LoadData()    
		{
			SqlConnection con = new SqlConnection("Data Source=GH0ST;Initial Catalog=InventoryProjectDB;Integrated Security=True");
			SqlDataAdapter sda = new SqlDataAdapter(@"SELECT * FROM [dbo].[PRODUCT]", con);
			DataTable dt = new DataTable();
			sda.Fill(dt);
			dataGridView1.Rows.Clear();
			foreach (DataRow item in dt.Rows)
			{
				int ar = dataGridView1.Rows.Add();
				dataGridView1.Rows[ar].Cells[0].Value = item["PID"].ToString();
				dataGridView1.Rows[ar].Cells[1].Value = item["PName"].ToString();
				dataGridView1.Rows[ar].Cells[2].Value = item["PPrice"].ToString();
				if ((bool)item["PStatus"])
				{
					dataGridView1.Rows[ar].Cells[3].Value = "In Stock";
				}
				else
				{
					dataGridView1.Rows[ar].Cells[3].Value = "Out of Stock";
				}
				dataGridView1.Rows[ar].Cells[4].Value = item["PQTY"].ToString();
				dataGridView1.Rows[ar].Cells[5].Value = item["PDateAdded"].ToString();
			}
		}

		private void MAIN_Load(object sender, EventArgs e)
		{
			
			DateTimePicker1.Value = DateTime.Now;
			cbx_Status.SelectedIndex = 0;
			LoadData();

		}


TextChanged code
C#
private void bx_Search_TextChanged(object sender, EventArgs e)
		{
			SqlConnection con = new SqlConnection("Data Source=GH0ST;Initial Catalog=InventoryProjectDB;Integrated Security=True");
			con.Open();
			SqlDataAdapter sda = new SqlDataAdapter(@"SELECT * FROM [dbo].[PRODUCT] WHERE PNAME LIKE '%" + bx_Search.Text + "%' ", con);
			DataTable dt = new DataTable();
			sda.Fill(dt);
			dataGridView1.DataSource = dt;
			con.Close();
}


Before query:
[IMG01]
After query:
[IMG02]

What I have tried:

I am stuck and i really need to finish this project.
Posted
Updated 11-Mar-18 7:57am
v3

Sometimes it helps to set the .DataSource to Null first:
dataGridView1.DataSource = null;
Also see example here: DatGridView Filtering User Control[^]
 
Share this answer
 
v2
Few tips:

1.
Take a look at LoadData and on bx_Search_TextChanged routine. In both you create a connection, command, etc.
The main rule of programming is: if a piece of code is used twice (or more) - change it into subroutine!

2.
Never use concatened string as a query! This is a common reason of SQL Injection[^]. The best way is to use parameterized queries[^].

3.
Use computed column of DataTable to get result of comparison "PStatus" to true/false.

Finally, your
C#
public void LoadData(string search="*")    
{
    SqlConnection con = new SqlConnection("Data Source=GH0ST;Initial Catalog=InventoryProjectDB;Integrated Security=True");
    string sql = "SELECT * FROM [dbo].[PRODUCT]";
    if(search!=string.Empty && search!="*") sql = string.Format("{0} WHERE PName Like '%{1}%'", sql, search);
    SqlDataAdapter sda = new SqlDataAdapter(sql, con);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    //add expression column!
    DataColumn expressionColumn = new DataColumn("Stock", typeof(string), "IIf(PStatus=true,'In Stock','Out of Stock')");
    dt.Columns.Add(expressionColumn);
    expressionColumn.SetOrdinal(4);
    dataGridView1.DataSource = dt;
    }
}

private void bx_Search_TextChanged(object sender, EventArgs e)
{
    LoadData(bx_Search.Text)
}



For further details, please see:
Creating Expression Columns | Microsoft Docs[^]
DataColumn.Expression Property [^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900