Click here to Skip to main content
14,271,364 members
Rate this:
Please Sign up or sign in to vote.
hello, i have table with many column like plate no , type of the car , year , user ,
etc-------
and i want whatever i choose the check or radio with name type only column type will show and like that and i use sqldatasource i am new in asp.net and just want to learn

What I have tried:

i tried to make separate tables and give everyone one of them sql datasource but that didn't work and make it worse i know how to filter the value inside the column the data based on it but i want only view specific column based on radio or check thx
Posted
Updated 20-Mar-17 13:56pm
v2
Comments
Mehedi Shams 19-Mar-17 21:02pm
   
Hi Member 13044689,

It is very difficult to pursue the problem with what you said. Can you elaborate a little more, please? What I understood is:

1) You have a big table with car plates, types, years, owners etc.
2) You have checkboxes and/or radio buttons to choose something - what are you choosing? plate numbers, types or what?
3) You want to only view specific columns based on the checkbox/radio button selections. Is it like if type and owner is selected then you want to display the types and owners columns only?
4) What are you using to display? Grid or what?
Member 13044689 20-Mar-17 2:07am
   
hello, yes as i said i want have gridview table with many columns and radio button or checkbox with name of columns whatever u choose in radio button 1 or 2 choose it will appear only the columns u choose in radiobutton ( its like hide and show columns based on radio button or checkbox)

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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 = "";

    // Check all the checkboxes and see which ones are selected. Concatenate them in the selection string.
    foreach (CheckBox CBox in CheckGroupBox.Controls)
    {
        if (CBox.Checked)
            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)
    {
        if (RButton.Checked)
        {
            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]";
    else
        Cmd.CommandText = "SELECT * FROM [Car]";    // If none is selected, then select all columns.
            
    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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100