You are on page 1of 2

// if the user click the "|<" button

int numOfRows = dgv_Inventory.Rows.Count;


int index = dgv_Inventory.SelectedCells[0].RowIndex;
if (index != 0)
{
dgv_Inventory.CurrentCell = dgv_Inventory[0, 0];
}
// if the user click the "<<" button
int numOfRows = dgv_Inventory.Rows.Count - 1;
int index = dgv_Inventory.SelectedCells[0].RowIndex;

if (index != 0)
{
dgv_Inventory.CurrentCell = dgv_Inventory[0, index - 1];
}
// if the user click the ">>" button
int numOfRows = dgv_Inventory.Rows.Count - 1;
int index = dgv_Inventory.SelectedCells[0].RowIndex;

if (index < numOfRows)


{
dgv_Inventory.CurrentCell = dgv_Inventory[0, index + 1];
}
// if the user click the ">|" button
int numOfRows = dgv_Inventory.Rows.Count;
int index = dgv_Inventory.SelectedCells[0].RowIndex;

if (index == 0)
{
dgv_Inventory.CurrentCell = dgv_Inventory[0, numOfRows - 1];
}
Search button
try
{
string query;
connection.Open();
query = "select * from tb_products where (Item_Name= '" +
txt_Search.Text + "') or(Item_Quantity = '" + txt_Search.Text + "')";

MySqlCommand cmd = new MySqlCommand(query, connection);


MySqlDataAdapter data = new MySqlDataAdapter();
data.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
data.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dgv_Inventory.DataSource = bSource;
btn_Search.BackColor = Color.Gold;
data.Update(dbdataset);
cmd.ExecuteReader();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// if the user click the Sort button for Ascending and Descending
if (cmb_Sort.SelectedItem.Equals("Ascending"))
{
// for Ascending
try
{
Con = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +
"UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(Con);

connection.Open();
string query = "Select * from tb_products ORDER BY Item_Name
ASC";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteReader();
connection.Close();
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dgv_Inventory.DataSource = bSource;
btn_Sort.BackColor = Color.Gold;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
// for Descending
try
{
Con = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +
"UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(Con);
connection.Open();
string query = "Select * from tb_products ORDER BY Item_Name
DESC";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteReader();
connection.Close();
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dgv_Inventory.DataSource = bSource;
btn_Sort.BackColor = Color.Red;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

You might also like