Imports System.Data.SqlClient
Imports System.Data
Public Class ColumnSelection
Inherits System.Web.UI.Page
Dim connectionString As String = "YourConnectionStringHere"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Load initial data into the GridView with all columns visible.
BindGridViewWithAllColumns()
End If
End Sub
Protected Sub ShowSelectedColumn(ByVal sender As Object, ByVal e As EventArgs)
' Determine which checkbox was selected.
Dim selectedCheckBox As CheckBox = DirectCast(sender, CheckBox)
' Update the GridView's visibility based on the selected checkbox.
If selectedCheckBox.ID = "Column1CheckBox" AndAlso Column1CheckBox.Checked Then
DataGrid.Columns(0).Visible = True ' Show Column 1
DataGrid.Columns(1).Visible = False ' Hide Column 2
' Hide other columns as needed
ElseIf selectedCheckBox.ID = "Column2CheckBox" AndAlso Column2CheckBox.Checked Then
DataGrid.Columns(0).Visible = False ' Hide Column 1
DataGrid.Columns(1).Visible = True ' Show Column 2
' Hide other columns as needed
Else
' If no checkbox is selected or multiple checkboxes are selected,
' show all columns by default.
BindGridViewWithAllColumns()
End If
End Sub
Private Sub BindGridViewWithAllColumns()
Using connection As New SqlConnection(connectionString)
Dim sql As String = "SELECT * FROM YourTableNameHere"
Dim adapter As New SqlDataAdapter(sql, connection)
Dim dt As New DataTable()
Try
connection.Open()
adapter.Fill(dt)
DataGrid.DataSource = dt
DataGrid.DataBind()
Catch ex As Exception
' Handle the exception (e.g., display an error message)
End Try
End Using
End Sub
End Class