How to Search Data in the DataGridView Using RadioButton in C#

In this tutorial, I will teach how to search for data in the datagridview using radiobutton in c#. This program illustrates how the data change in the datagridview according to the selected radiobutton. This is very helpful for you if you want to retrieve a group of data in the database. I use Microsoft Visual Studio 2015 and MySQL Database to create this project.

Creating Database

Go to localhost/PHPMyAdmin/ and create a database named “tuts_persondb” Write the following query to create a table.
  1.  
  2. CREATE TABLE `tblperson` (
  3. `PersonID` int(11) NOT NULL,
  4. `Fullname` varchar(90) NOT NULL,
  5. `CivilStatus` varchar(30) NOT NULL
Write the following query to insert the data in the table
  1.  
  2. INSERT INTO `tblperson` (`PersonID`, `Fullname`, `CivilStatus`) VALUES
  3. (1, 'Janobe Sourcecode', 'Single'),
  4. (2, 'Mark Lim', 'Married'),
  5. (3, 'Jake Cueca', 'Widow');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application c#. figure 1

Step 2

Add a GroupBox, two RadioButtons, and a DataGridView in the form. After that, design the form just like shown below. figure 2

Step 3

Add MySQL.data.dll

Step 4

Press F7 to open the code editor. After that, add a namespace to access MySQL libraries.
  1. using MySql.Data.MySqlClient;

Step 5

Inside the class, create a connection between MySQL Database and C#. After that, declare all the classes that are needed.
  1.  
  2. MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=tuts_persondb;sslMode=none");
  3. MySqlCommand cmd;
  4. MySqlDataAdapter da;
  5. DataTable dt;
  6. string sql;

Step 5

Create a method for retrieving data in the database.
  1.  
  2. private void loaddata(string sql,DataGridView dtg)
  3. {
  4. try
  5. {
  6. con.Open();
  7.  
  8. cmd = new MySqlCommand();
  9. da = new MySqlDataAdapter();
  10. dt = new DataTable();
  11.  
  12. cmd.Connection = con;
  13. cmd.CommandText = sql;
  14. da.SelectCommand = cmd;
  15. da.Fill(dt);
  16.  
  17. dtg.DataSource = dt;
  18.  
  19. }
  20. catch(Exception ex)
  21. {
  22. MessageBox.Show(ex.Message);
  23. }
  24. finally
  25. {
  26. con.Close();
  27. da.Dispose();
  28. }
  29. }

Step 6

Do the following codes for retrieving data in the first load of the form.
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. sql = "Select * From tblperson";
  4. loaddata(sql, dataGridView1);
  5. }

Step 7

Do the following codes for finding records when the radio button is checked.
  1. private void rdoSingle_CheckedChanged(object sender, EventArgs e)
  2. {
  3. sql = "Select * From tblperson WHERE CivilStatus = '" + rdoSingle.Text + "'";
  4. loaddata(sql, dataGridView1);
  5. }
  6.  
  7. private void btnMarried_CheckedChanged(object sender, EventArgs e)
  8. {
  9. sql = "Select * From tblperson WHERE CivilStatus = '" + rdoMarried.Text + "'";
  10. loaddata(sql, dataGridView1);
  11. }
  12.  
  13. private void btnWidow_CheckedChanged(object sender, EventArgs e)
  14. {
  15. sql = "Select * From tblperson WHERE CivilStatus = '" + rdoWidow.Text + "'";
  16. loaddata(sql, dataGridView1);
  17. }
Download the complete source code and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment