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
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);
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 [
^]