Hi
Member 13044689
,
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
Tag
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 = "";
foreach (CheckBox CBox in CheckGroupBox.Controls)
{
if (CBox.Checked)
SBuilder.Append(CBox.Tag + ", ");
}
foreach (RadioButton RButton in RadioButtonGroupBox.Controls)
{
if (RButton.Checked)
{
SBuilder.Append(RButton.Tag + ", ");
break;
}
}
if (SBuilder.ToString().Length > 0)
SelectionString = SBuilder.ToString().Substring(0, SBuilder.ToString().Length - 2);
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]";
else
Cmd.CommandText = "SELECT * FROM [Car]";
DataTable DT = new DataTable();
SqlDataAdapter DAdapter = new SqlDataAdapter(Cmd);
DAdapter.Fill(DT);
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.