” hide a gridview column if all the row values for that column are null”


Here is a code snippet I posted  before:

Here 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

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s