So here is the Final Solution
What I did to get the required
Results Hope this helps
other Also :
the First part what I have done to get the Checked
Fields under a String Separated by commas
Public Sub getCheckedValues()
Dim sb As New StringBuilder
Dim i As Integer
For i = 0 To chkBox.Items.Count - 1
If chkBox.GetItemChecked(i) = True Then
sb.Append(chkBox.Items(i).ToString & ",")
End If
Next
Dim str As String = sb.ToString.Trim(",")
and then As Rob suggested,Used a Dynamic Stored Procedure to fetch the Data and then loaded them to The DataGridView
Dim con As New SqlConnection
Dim cmd As New SqlCommand
With con
.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=New1;Integrated Security=True;Pooling=False"
.Open()
End With
Dim s As String = cmbTabView.Text.ToString
Dim o As String = cmbColom.Text.ToString
Dim sCconnect = "Data Source=.\sqlexpress;Initial Catalog=New1;Integrated Security=True;Pooling=False"
Dim xSqlConnection As SqlConnection = New SqlConnection(sCconnect)
Dim xSqlCommand As SqlCommand = New SqlCommand("DynamicStoredProc", xSqlConnection)
xSqlCommand.CommandType = CommandType.StoredProcedure
xSqlCommand.Parameters.AddWithValue("@Select", str)
xSqlCommand.Parameters(0).SqlDbType = SqlDbType.VarChar
xSqlCommand.Parameters.AddWithValue("@Table", s)
xSqlCommand.Parameters(1).SqlDbType = SqlDbType.VarChar
xSqlCommand.Parameters.AddWithValue("@Order", o)
xSqlCommand.Parameters(2).SqlDbType = SqlDbType.VarChar
xSqlCommand.Connection.Open()
Dim xSqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(xSqlCommand)
Dim dt As New DataTable
xSqlDataAdapter.Fill(dt)
dgvData.DataSource = dt
End Sub
called This Function on Show Button Click.Here I also added Order by Clause to Sort the Data.Structure of Stored Procedure what I used is as
ALTER PROCEDURE dbo.DynamicStoredProc
@Select varchar(500) = '',
@Table varchar(500) = '',
@Order varchar(500)=''
AS
SET NOCOUNT ON;
DECLARE @Query varchar(500)
SET @Query = 'SELECT ' + @Select + ' FROM ' + @Table + ' ORDER BY ' + @Order
EXEC(@Query)
RETURN
That's all.And Yes Don't Forget To import System.Text namespace which is needed for StringBuilder Class.