You didn't specify what data source you are using. I am guessing it is SQL Server.
The logic is:
1) On form load, display all columns (or it is up to you if you don't want to display).
2) Create a button and put the refined columns code in the click event.
3) Put your checkboxes in a group box, put the radio buttons in another group box. You can name them differently than the name they have in database. But I am using the
property where I put the exact column name that it has in database.
4) Loop through your checkboxes and concatenate the selections in a selection string.
5) Loop through your radio buttons and concatenate the selections in a selection string.
6) Create an SQL command based on the selections. If none selected, then select all.
7) Set your grid's data source to the new data table.
private void DisplaySelectedColsBtn_Click(object sender, EventArgs e)
StringBuilder SBuilder = new StringBuilder();
string SelectionString = "";
// Check all the checkboxes and see which ones are selected. Concatenate them in the selection string.
foreach (CheckBox CBox in CheckGroupBox.Controls)
SBuilder.Append(CBox.Tag + ", ");
// Check all the radio buttons and see which one is selected. Concatenate them in the selection string.
foreach (RadioButton RButton in RadioButtonGroupBox.Controls)
SBuilder.Append(RButton.Tag + ", "); // Comma is still needed as a trim operation is waiting up ahead.
break; // For radio buttons, only one column is selectable. So, once a selection is found, then no need to loop any further.
// Eliminate the last comma if at all any checkbox is selected.
if (SBuilder.ToString().Length > 0)
SelectionString = SBuilder.ToString().Substring(0, SBuilder.ToString().Length - 2);
// Modify the connection string as appropriate.
SqlConnection Conn = new SqlConnection("Server=.;Integrated Security=SSPI;Initial Catalog=WorkBench");
SqlCommand Cmd = new SqlCommand();
Cmd.Connection = Conn;
if (SelectionString.Length > 0)
Cmd.CommandText = "SELECT " + SelectionString + " FROM [Car]";
Cmd.CommandText = "SELECT * FROM [Car]"; // If none is selected, then select all columns.
DataTable DT = new DataTable();
SqlDataAdapter DAdapter = new SqlDataAdapter(Cmd);
CarsGridView.DataSource = DT;
Disclaimer: This code is vulnerable to SQL injection attack. I don't know your working scenario. Suggestion is to create an SP and pass to it the selected columns; so it can return the selected data.