I'd move that code out of Form2_Load and into a new method in Form2 - RefreshGrid, say.
internal void RefreshGrid()
{
string bookname = form1.bname();
string catcode = form1.category();
LibraryLib.Library obj = new LibraryLib.Library();
DataTable dt;
dt = obj.select("select Book_no, E_Name from Books where E_Name like'" + bookname.ToUpper() + "%' and Catcode='"+catcode+"'");
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
Your code in Form2_Load can then be reduced to calling this method:
private void Form2_Load(object sender, EventArgs e)
{
RefreshGrid();
}
Finally, a change is needed to Form1_TextChanged to call the RefreshGrid method when Form2 is already open:
private void textBox1_TextChanged(object sender, EventArgs e)
{
bookname = textBox1.Text;
if (obj == null)
{
obj = new Form2();
obj.Show();
textBox1.Leave += textBox1_Leave; // add handler
textBox1.Focus();
}
else
{
obj.RefreshGrid();
}
}