Hi ilanocf,
Refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table>
<tr>
<td>
<asp:ListBox runat="server" ID="lstColumns" AutoPostBack="true"
OnSelectedIndexChanged="OnSelectedIndexChanged" SelectionMode="Multiple">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
</asp:ListBox>
</td>
<td>
<asp:ListBox runat="server" ID="lstSelectedColumns"></asp:ListBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button Text="Search" runat="server" OnClick="OnSearch" /></td>
</tr>
</table>
<asp:GridView runat="server" ID="gvCustomers"></asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
lstColumns.DataSource = GetData("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'Customers'");
lstColumns.DataTextField = "COLUMN_NAME";
lstColumns.DataValueField = "COLUMN_NAME";
lstColumns.DataBind();
}
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
lstSelectedColumns.Items.Clear();
foreach (ListItem item in lstColumns.Items)
{
if (item.Selected)
{
lstSelectedColumns.Items.Add(new ListItem { Text = item.Text, Value = item.Value });
}
}
}
protected void OnSearch(object sender, EventArgs e)
{
List<string> columns = new List<string>();
foreach (ListItem item in lstSelectedColumns.Items)
{
columns.Add(item.Text);
}
string query = string.Format("SELECT TOP 5 {0} FROM Customers", string.Join(",", columns));
gvCustomers.DataSource = GetData(query);
gvCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lstColumns.DataSource = GetData("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'Customers'")
lstColumns.DataTextField = "COLUMN_NAME"
lstColumns.DataValueField = "COLUMN_NAME"
lstColumns.DataBind()
End If
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
lstSelectedColumns.Items.Clear()
For Each item As ListItem In lstColumns.Items
If item.Selected Then
lstSelectedColumns.Items.Add(New ListItem With {
.Text = item.Text,
.Value = item.Value
})
End If
Next
End Sub
Protected Sub OnSearch(ByVal sender As Object, ByVal e As EventArgs)
Dim columns As List(Of String) = New List(Of String)()
For Each item As ListItem In lstSelectedColumns.Items
columns.Add(item.Text)
Next
Dim query As String = String.Format("SELECT TOP 5 {0} FROM Customers", String.Join(",", columns))
gvCustomers.DataSource = GetData(query)
gvCustomers.DataBind()
End Sub
Screenshot