Don’t show item when the value is NULL in a DetailsView


Sometimes you don’t want to show the item if the datum is null on your DetailsView. You can check the value in your detailsview and change the visiblity of that row which includes the item. Here is the code to show how to do it through databound event:
protected void DetailsView1_OnDataBound(object sender, EventArgs e)
    {
        
        
            if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
            {
                //DetailsViewMode.ReadOnly or DetailsViewMode.Edit 

                System.Data.DataRowView rowView = (System.Data.DataRowView)DetailsView1.DataItem;


                //if (rowView.Row[2].ToString() == "") //use column index 
                if (rowView.Row["nullableCol"].ToString() == "") //use column name
                {

                    DetailsView1.Rows[2].Visible = false;//the column has null values

                }


            }
        } 

 

VB.NET:

 Protected Sub DetailsView1_OnDataBound(ByVal sender As Object, ByVal e As EventArgs) Handles DetailsView1.DataBound

       
        If DetailsView1.CurrentMode = DetailsViewMode.ReadOnly Then
            'DetailsViewMode.ReadOnly or DetailsViewMode.Edit 

            Dim rowView As System.Data.DataRowView = _
                             CType(DetailsView1.DataItem, System.Data.DataRowView)


            If rowView.Row(2).ToString() = "" Then

                DetailsView1.Rows(2).Visible = False

            End If

        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