我创建了一个下拉列表控制器和gridview控制器。
gridview填充(显示)取决于下拉列表没有问题的值。
在我的sql表中,我有3列在gridview中,我想要做的是在gridview中只显示包含任何行数据的列。
例如:
我在下拉列表值1中选择然后显示Kolumn 1,3,因为那些包含行数据和colum 2为NULL。
我在下拉列表值2中选择,然后第2列将显示,因为它包含行数据并且列1,3为空
在我的下拉列表中,我有代码隐藏来打印下拉列表
在gridview中我有aspx代码。
更新了代码: 这是我的ASPX代码。
<asp:GridView ID="FinanceGridView" runat="server" AutoGenerateColumns="False" CssClass = "FinanceGridView"
DataKeyNames="ID" DataSourceID="AdminPortal_ReportScheduling"
AllowPaging="True" AllowSorting="True" Visible="False">
<Columns>
<asp:BoundField DataField="Report" HeaderText="Report"
SortExpression="Report" />
<asp:BoundField DataField="Business_Area" HeaderText="Business_Area"
SortExpression="Business_Area" />
<asp:BoundField DataField="Salesdepartment" HeaderText="Salesdepartment"
SortExpression="Salesdepartment" />
<asp:CommandField ButtonType="Button"
ShowEditButton="True">
<ControlStyle BackColor="Black" ForeColor="White" />
</asp:CommandField>
</Columns>
</asp:GridView>
新的正确代码?
Dim SqlConnectionString As String = ConfigurationManager.ConnectionStrings("Test").ConnectionString
Protected Sub BindDataToGridView()
Using SqlConnection As New SqlConnection(SqlConnectionString)
SqlConnection.Open()
Dim SqlCommand As New SqlCommand("Select ID,Report,[Business Area],[Salesdepartment] from dbo.test", SqlConnection)
Dim SqlDataAdapter As New SqlDataAdapter(SqlCommand)
Dim dataSet As New DataSet()
SqlDataAdapter.Fill(dataSet)
For Each col As DataControlField In Gridview1.Columns
col.Visible = False
Next
For Each row As GridViewRow In Gridview1.Rows
For i As Integer = 0 To row.Cells.Count - 1
If row.Cells(i).Text.Replace(" ", String.Empty) <> String.Empty Then
Gridview1.Columns(i).Visible = True
End If
Next
Next
SqlConnection.Close()
End Using
End Sub
答案 0 :(得分:0)
如果您创建了自己的列并删除了AutoGenerateColumns,则可以这样做:
Protected Sub gvData_DataBound(sender As Object, e As EventArgs)
For Each col As DataControlField in gvData.Columns
col.Visible = false
Next
For Each row as GridViewRow in gvData.Rows
For i As Integer = 0 to row.Cells.Count - 1
If row.Cells(i).Text.Replace(" ", string.Empty) <> string.Empty Then
gvData.Columns(i).Visible = True
End If
Next
Next
End Sub
使用自动生成的列,这应该有效:
Protected Sub gvData_DataBound(sender as Object sender, e As EventArgs)
Dim cols As New List(Of Boolean)
For Each row As GridViewRow in gvData.Rows
For i as Integer = 0 to row.Cells.Count - 1
If cols.Count <= i
cols.Add(False)
End If
If row.Cells(i).Text.Replace(" ", string.Empty) <> string.Empty Then
cols(i) = True
End If
Next
Next
For i as Integer = 0 to gvData.HeaderRow.Cells.Count - 1
gvData.HeaderRow.Cells(i).Visible = cols(i)
Next
For Each row As GridViewRow in gvData.Rows
For i As Integer = 0 to row.Cells.Count - 1
row.Cells(i).Visible = cols(i)
Next
Next
End Sub
所有这些仅适用于显示文本的字段(不适用于显示为复选框的布尔字段)。我希望我对VB的翻译没问题。