I am trying to query the columns from which data will be retrieved based on the selected item with checkedListBox. But I couldn't understand the logic. Would you help me with this topic? I am using c#.

I am trying to query the columns from which data will be retrieved based on the selected item with checkedListBox. But I couldn't understand the logic. Would you help me with this topic? I am using c#.

Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 26, 2023, 3:16 PM EDT
To query columns from a database based on the selections made in a
CheckedListBoxin C#, you can use the following example as a reference. (I'm assuming you are using SQL database)using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace CheckedListBoxExample { public partial class Form1 : Form { private const string connectionString = "YOUR_CONNECTION_STRING"; // Replace with your actual connection string public Form1() { InitializeComponent(); PopulateCheckedListBox(); // Populates the CheckedListBox with column names } private void PopulateCheckedListBox() { using (var connection = new SqlConnection(connectionString)) { string query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YOUR_TABLE_NAME'"; // Replace with your actual table name var command = new SqlCommand(query, connection); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { checkedListBox1.Items.Add(reader["COLUMN_NAME"].ToString()); } } } private void button1_Click(object sender, EventArgs e) { List selectedColumns = new List(); foreach (var item in checkedListBox1.CheckedItems) { selectedColumns.Add(item.ToString()); } using (var connection = new SqlConnection(connectionString)) { string query = "SELECT " + string.Join(", ", selectedColumns) + " FROM YOUR_TABLE_NAME"; // Replace with your actual table name var command = new SqlCommand(query, connection); connection.Open(); var adapter = new SqlDataAdapter(command); var table = new DataTable(); adapter.Fill(table); dataGridView1.DataSource = table; } } } }Mehmet FatihRank #1,475 · 1.1K reputationPosted Oct 26, 2023, 4:18 PM EDT
Thanks for your intrest, Muhammed Imran Ansari. When I try your codes, I am getting this error.
How can we send hidden checkboxlist value?