Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Can some one tell me why this code is giving me these error:

<pre lang="msil">Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 34:         else
Line 35:         {
Line 36:             grdAvailableStuff.HeaderRow.Cells[2].Visible = false;
Line 37:
Line 38:</pre>


Code below

<pre lang="cs">protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            radDoctors.Checked = false;
            radNurses.Checked = false;
        }
        if (grdAvailableStuff.Columns.Count == 0)
        {
            grdAvailableStuff.Columns[2].Visible = false;


        }
        else
        {
            grdAvailableStuff.HeaderRow.Cells[2].Visible = false;


            foreach (GridViewRow grd in grdAvailableStuff.Rows)
            {
                grd.Cells[2].Visible = false;


            }
        }

    }</pre>
Posted

Do this in the RowCreated method. You can use code similar to the following:

C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Visible =
            e.Row.Cells[1].Visible =
            e.Row.Cells[3].Visible = false;

        // and so on and so forth...
    }
}


You can manipulate data and cells in the RowDataBound event.
 
Share this answer
 
Comments
Anele Ngqandu 16-Jun-11 9:51am    
But intelisense cant event find RowDataBound
R. Giskard Reventlov 16-Jun-11 9:55am    
In the properties grid for the GridView it is marked as an event (in the event tab of the control - the little button with the lightning in it). Double-Click on RowDataBound and it the method will be created for you.
Anele Ngqandu 16-Jun-11 10:08am    
yes sir but its only hiding the header not the whole column
Anele Ngqandu 16-Jun-11 10:17am    
ok this is what i tried see below:

if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Cells[3].Visible = false;


}
if (e.Row.RowType == DataControlRowType.Header)
{

e.Row.Cells[3].Visible = false;


}
R. Giskard Reventlov 16-Jun-11 10:33am    
That is correct though you can combine with:
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
you can also do this in .aspx page using

if condition
set the global variable to the .cs file

check the value of that global variable using if condition
and you can visible or invisible that column accordingly.
 
Share this answer
 
I suggest that go via ColumnName not via Index

if (GridView1.Columns["FieldID"] != null) GridView1.Columns["FieldID"].Visible = false;


And for setting visibility property for the columns, no need to use RowDataBound event. Directly after binding the grid call the above line
 
Share this answer
 
Comments
Anele Ngqandu 16-Jun-11 10:13am    
it doesnt allow me to put strings inside square bruckets
set 'Visible=False' property as like this

u can hide any control on the page
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900