G
Guest
I have been trying for the last two weeks to display a dynamic DataGrid. The
data that I'm pulling from a SQL Server DB will have whole columns that will
be either NULL or 0. I want to display only the fields that have actual data
in them and either not display or hide the columns that have NULL or 0
values. As an example, one page may need to display a total of 5 columns and
another page may need to display three columns based on the product category
the user chooses. I have done this in classic ASP, but I can't figure out
how to do it with the DataGrid and ASP.NET.
The code I have so far looks like this.
Dim strFamily as String
strFamily = Request.QueryString("fam")
Dim strConnection as String, sqlConn as SQLConnection
strFamily = Request.QueryString("fam")
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
Dim MyDataAdapter as SQLDataAdapter, MyDataSet as New DataSet
MyDataAdapter = New SQLDataAdapter("SELECT * FROM Items WHERE Part = '" &
Request.QueryString("part") & "'", strConnection)
MyDataAdapter.Fill(MyDataSet, "Items")
Dim i as Integer
'To navigate through the records.
For i = 0 To MyDataSet.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()
objbc.DataField = MyDataSet.Tables(0).Columns(i).ColumnName
objbc.HeaderText = MyDataSet.Tables(0).Columns(i).ColumnName
If objbc.DataField = MyDataSet.Tables(0).Columns("Part").ColumnName Then
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Columns("Footnote").ColumnName Then
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Colum("Packaged").ColumnNameThen
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Column("PkgQuantity").ColumnNameThen
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Columns("PrintName").ColumnName Then
objbc.Visible = False
End If
dgItemDetail.Columns.Add(objbc)
dgItemDetail.DataSource = MyDataSet.Tables(0)
dgItemDetail.DataBind()
Next 'i
Any help is greatly appreciated. I'm already losing my hair and the last
few days it hasn't been getting any better!
data that I'm pulling from a SQL Server DB will have whole columns that will
be either NULL or 0. I want to display only the fields that have actual data
in them and either not display or hide the columns that have NULL or 0
values. As an example, one page may need to display a total of 5 columns and
another page may need to display three columns based on the product category
the user chooses. I have done this in classic ASP, but I can't figure out
how to do it with the DataGrid and ASP.NET.
The code I have so far looks like this.
Dim strFamily as String
strFamily = Request.QueryString("fam")
Dim strConnection as String, sqlConn as SQLConnection
strFamily = Request.QueryString("fam")
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
Dim MyDataAdapter as SQLDataAdapter, MyDataSet as New DataSet
MyDataAdapter = New SQLDataAdapter("SELECT * FROM Items WHERE Part = '" &
Request.QueryString("part") & "'", strConnection)
MyDataAdapter.Fill(MyDataSet, "Items")
Dim i as Integer
'To navigate through the records.
For i = 0 To MyDataSet.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()
objbc.DataField = MyDataSet.Tables(0).Columns(i).ColumnName
objbc.HeaderText = MyDataSet.Tables(0).Columns(i).ColumnName
If objbc.DataField = MyDataSet.Tables(0).Columns("Part").ColumnName Then
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Columns("Footnote").ColumnName Then
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Colum("Packaged").ColumnNameThen
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Column("PkgQuantity").ColumnNameThen
objbc.Visible = False
End If
If objbc.DataField = MyDataSet.Tables(0).Columns("PrintName").ColumnName Then
objbc.Visible = False
End If
dgItemDetail.Columns.Add(objbc)
dgItemDetail.DataSource = MyDataSet.Tables(0)
dgItemDetail.DataBind()
Next 'i
Any help is greatly appreciated. I'm already losing my hair and the last
few days it hasn't been getting any better!