” hide a gridview column if all the row values for that column are null”
Posted: October 10, 2010 Filed under: ASP.NET 2 Leave a commentHere is working sample from a previous thread:
1.Add these two events to the gridview
OnRowCreated=”GridView1_OnRowCreated”
OnDataBound=”GridView1_DataBound”
2.In the code behind:
C#:
Boolean[] notNulls;
protected void Page_Load(object sender, EventArgs e)
{
int colCount = GridView1.Columns.Count;
notNulls = new Boolean[colCount];
for (int i = 0; i < colCount; i++)
{
notNulls[i]=false;
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
for (int cellNum = GridView1.Columns.Count – 1; cellNum >= 0; cellNum–)
{
for (int i = 0; i < notNulls.Length; i++)
{
Boolean myLocalBool = notNulls[i];
if (myLocalBool == false)
{
GridView1.Columns[i].Visible = false;
}
}
}
}
protected void GridView1_OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
for (int i = 0; i < GridView1.Columns.Count; i++)
{
if (!(drv[i] == DBNull.Value))
{
notNulls[i] = true;
}
}
}
}
VB.NET:
Dim notNulls As Boolean()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim colCount As Integer = GridView1.Columns.Count
notNulls = New Boolean(colCount) {}
Dim i As Integer = 0
While i < colCount
notNulls(i) = False
i = i + 1
End While
End Sub
Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim cellNum As Integer = GridView1.Columns.Count – 1
While cellNum > 0
Dim i As Integer = 0
While i < GridView1.Columns.Count ‘Changed from notNulls.Length in C#
Dim myLocalBool As Boolean = notNulls(i)
If myLocalBool = False Then
GridView1.Columns(i).Visible = False
End If
i = i + 1
End While
cellNum = cellNum – 1
End While
End Sub
Protected Sub GridView1_OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim i As Integer = 0
While i < GridView1.Columns.Count
If Not drv(i) Is DBNull.Value Then
notNulls(i) = True
End If
i = i + 1
End While
End If
End Sub
List file info into a database table
Posted: October 7, 2010 Filed under: SQL Server 2005 Leave a commentALTER
PROCEDURE [dbo].[GetDirDetails_Jeff_Moden]–http://www.sqlservercentral.com/Forums/Topic540627-338-1.aspx
/**************************************************************************************************
Purpose:
Replacement code for the sp_GetFileDetails proc that was available in SQL Server 2000
which is no longer available in SQL Server 2005 except this gets all the file details
for all the files in the directory provided.
Notes:
1. If the directory does not exist or there are no files in the directory, an empty
result set will be returned.
2. If the trailing backslash on the path is missing, it will be added automatically.
3. No error checking is done. Either a valid result set is returned or an empty
result set is returned (much like a function operates).
Usage:
EXEC dbo.GetDirDetails ‘drive:path or UNC’
Revision History:
Rev 00 – 05/23/2008 – Jeff Moden
– Initial concept borrowed from Simple-Talk.com (Phil Factor) and
modified for multiple files.
Rev 01 – 05/25/2008 – Jeff Moden
– Formalize the code for use.
**************************************************************************************************/
–===== Declare the I/O parameters
@piFullPath
VARCHAR(128) AS–===== Suppress the auto-display of rowcounts so as not to interfere with the returned
— result set SET NOCOUNT ON–=================================================================================================
— Local variables
–=================================================================================================
–===== These are processing control and reporting variables
DECLARE
@Counter INT –General purpose counterDECLARE
@CurrentName VARCHAR(256) –Name of file currently being workedDECLARE
@DirTreeCount INT –Remembers number of rows for xp_DirTreeDECLARE
@IsFile BIT –1 if Name is a file, 0 if not–===== These are object "handle" variables
DECLARE
@ObjFile INT –File objectDECLARE
@ObjFileSystem INT –File System Object–===== These variable names match the sp_OAGetProperty options
— Made names match so they’re less confusingDECLARE
@Attributes INT –Read only, Hidden, Archived, etc, as a bit mapDECLARE
@DateCreated DATETIME –Date file was createdDECLARE
@DateLastAccessed DATETIME –Date file was last read (accessed)DECLARE
@DateLastModified DATETIME –Date file was last written toDECLARE
@Name VARCHAR(128) –File Name and ExtensionDECLARE
@Path VARCHAR(128) –Full path including file nameDECLARE
@ShortName VARCHAR(12) –8.3 file nameDECLARE
@ShortPath VARCHAR(100) –8.3 full path including file nameDECLARE
@Size INT –File size in bytesDECLARE
@Type VARCHAR(100) –Long Windows file type (eg.’Text Document’,etc)–=================================================================================================
— Create temporary working tables
–=================================================================================================
–===== Create a place to store all file names derived from xp_DirTree
IF OBJECT_ID(‘TempDB..#DirTree’,‘U’) IS NOT NULL DROP TABLE #DirTree CREATE TABLE #DirTree(
RowNum
INT IDENTITY(1,1),Name
VARCHAR(256) PRIMARY KEY CLUSTERED,Depth
BIT,IsFile
BIT)
–===== Create a place to store the file details so we can return all the file details
— as a single result set IF OBJECT_ID(‘TempDB..#FileDetails’,‘U’) IS NOT NULL DROP TABLE #FileDetails IF OBJECT_ID(‘FileDetails’,‘U’) IS NOT NULL DROP TABLE FileDetails CREATE TABLE FileDetails(
RowNum
INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,Name
VARCHAR(128), –File Name and Extension Path VARCHAR(128), –Full path including file nameShortName
VARCHAR(12), –8.3 file nameShortPath
VARCHAR(100), –8.3 full path including file nameDateCreated
DATETIME, –Date file was createdDateLastAccessed
DATETIME, –Date file was last readDateLastModified
DATETIME, –Date file was last written toAttributes
INT, –Read only, Compressed, ArchivedArchiveBit
AS CASE WHEN Attributes& 32=32 THEN 1 ELSE 0 END,CompressedBit
AS CASE WHEN Attributes&2048=2048 THEN 1 ELSE 0 END,ReadOnlyBit
AS CASE WHEN Attributes& 1=1 THEN 1 ELSE 0 END,Size
INT, –File size in bytes Type VARCHAR(100) –Long Windows file type (eg.’Text Document’,etc))
–=================================================================================================
— Make sure the full path name provided ends with a backslash
–=================================================================================================
SELECT @piFullPath = @piFullPath+‘\’ WHERE RIGHT(@piFullPath,1)<>‘\’–=================================================================================================
— Get all the file names for the directory (includes directory names as IsFile = 0)
–=================================================================================================
–===== Get the file names for the desired path
— Note that xp_DirTree is available in SQL Server 2000, 2005, and 2008. INSERT INTO #DirTree (Name, Depth, IsFile) EXEC Master.dbo.xp_DirTree @piFullPath,1,1 — Current diretory only, list file names — Remember the row count SET @DirTreeCount = @@ROWCOUNT–===== Update the file names with the path for ease of processing later on
UPDATE #DirTree SET Name = @piFullPath + Name–=================================================================================================
— Get the properties for each file. This is one of the few places that a WHILE
— loop is required in T-SQL.
–=================================================================================================
–===== Create a file system object and remember the "handle"
EXEC dbo.sp_OACreate ‘Scripting.FileSystemObject’, @ObjFileSystem OUT–===== Step through the file names and get the properties for each file.
SET @Counter = 1 WHILE @Counter <= @DirTreeCount BEGIN –===== Get the current name and see if it’s a file SELECT @CurrentName = Name,@IsFile
= IsFile FROM #DirTree WHERE RowNum = @Counter –===== If it’s a file, get the details for it IF @IsFile = 1 AND @CurrentName LIKE ‘%%’ BEGIN –===== Create an object for the path/file and remember the "handle" EXEC dbo.sp_OAMethod @ObjFileSystem,‘GetFile’, @ObjFile OUT, @CurrentName –===== Get the all the required attributes for the file itself EXEC dbo.sp_OAGetProperty @ObjFile, ‘Path’, @Path OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘ShortPath’, @ShortPath OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘Name’, @Name OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘ShortName’, @ShortName OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘DateCreated’, @DateCreated OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘DateLastAccessed’, @DateLastAccessed OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘DateLastModified’, @DateLastModified OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘Attributes’, @Attributes OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘Size’, @Size OUT EXEC dbo.sp_OAGetProperty @ObjFile, ‘Type’, @Type OUT –===== Insert the file details into the return table INSERT INTO FileDetails (Path, ShortPath, Name, ShortName, DateCreated,DateLastAccessed
, DateLastModified, Attributes, Size, Type) SELECT @Path,@ShortPath,@Name,@ShortName,@DateCreated,@DateLastAccessed
,@DateLastModified,@Attributes,@Size,@Type END –===== Increment the loop counter to get the next file or quit SELECT @Counter = @Counter + 1 END–===== House keeping, destroy and drop the file objects to keep memory leaks from happening
EXEC sp_OADestroy @ObjFileSystem EXEC sp_OADestroy @ObjFile–===== Return the details for all the files as a single result set.
— This is one of the few places in T-SQL where SELECT * is ok. SELECT * FROM FileDetails