0
Thanks for your intrest, Muhammed Imran Ansari. When I try your codes, I am getting this error.

How can we send hidden checkboxlist value?
0
To query columns from a database based on the selections made in a CheckedListBox
in 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<string> selectedColumns = new List<string>();
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;
}
}
}
}
