4 Replies - 4878 Views - Last Post: 25 September 2012 - 05:56 AM Rate Topic: -----

#1 diorno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-September 12

How to hide Gridview Column in asp.net...?

Posted 24 September 2012 - 05:32 AM

Hi all!
I need some help about to hide 1 or more Gridview Columns in asp.net.
What i am trying to do, is that i want to hide a GridView Column deepending on the type of the user (Administrator or Standart).

I tryed this code:

  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection Conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["KlientiStrConn"].ConnectionString);
        Conn1.Open();
            string SqlQry1 = "select TipiPedoruesi from Perdoruesi where Emri = '" + TxtBox1.text + "'";
            SqlCommand SqlCmd1 = new SqlCommand(SqlQry1, Conn1);
            string y = SqlCmd1.ExecuteScalar().ToString();
        Conn1.Close();
            if (y == "Administrator") 
                {
                    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowtype.Header); 
                        {
                            e.Row.Cells[1].Visible = true;
                        }
                }
            else if(y == "Standart")
                {
                    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowtype.Header); 
                        {
                            e.Row.Cells[1].Visible = false;
                        }
                }

    }


But it shows to me a lot of errors.
I have made a print screen of the errors.

Thank you in advance...!!!

Is This A Good Question/Topic? 0
  • +

Replies To: How to hide Gridview Column in asp.net...?

#2 h4nnib4l  Icon User is online

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: How to hide Gridview Column in asp.net...?

Posted 24 September 2012 - 07:30 AM

Will you post the print screen of the errors? And onselectedIndexChanged seems like a strange place to handle display logic. Is the user type changing on the event? If not, OnRowDataBound seems like a better idea to me.
Was This Post Helpful? 0
  • +
  • -

#3 diorno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-September 12

Re: How to hide Gridview Column in asp.net...?

Posted 24 September 2012 - 08:06 AM

Here is the print screen of the errors...!!!

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 396
  • Joined: 04-January 11

Re: How to hide Gridview Column in asp.net...?

Posted 25 September 2012 - 01:50 AM

Wow. That's a lot of errors you got there.

I am also confused why you placed that in onselectedindexchanged. Just like h4nnib4l said, you hide the columns you need to hide in onrowdatabound. But the logic of the whole code you posted is a lot confusing. Why don't you change the logic of your program?

1. It causes a lot of roundtrips to the server if you are going to check user role on every selectedindexchanged. What is TxtBox1 for? Do you have a user login?

It is better that on login, you store the user details, the username and role in a Session. You can access this session all throughout your site on runtime.

2. I assume you know how to bind datasource to gridview. To dynamically show or hide columns, add codes in the OnRowDataBound event. This event will be executed once you call yourGridView.DataBind();

You check the value inside the session in which you stored the userrole.

Example:
switch (Session["userRole"].ToString())
{
 case "Administrator":
   //some codes here
   break;
 case "Standard":
  //some codes here
  break;
}


These two are the basic things you need for this kind of program. Hope you do well to revise your codes.

This post has been edited by November-06: 25 September 2012 - 01:53 AM

Was This Post Helpful? 1
  • +
  • -

#5 h4nnib4l  Icon User is online

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: How to hide Gridview Column in asp.net...?

Posted 25 September 2012 - 05:56 AM

I apparently left all of this in the reply box last night...

Okay, what have you tried to resolve each error? Let's take the 3rd item (1st error) on the list, for example. Which line of code is it associated with? What object are you implicitly trying to cast as a string? Look at these errors, one at a time, and see what you can do to resolve each one. Check this tutorial out for help debugging your code. When you get to the point where you just can't figure out a specific problem (not a laundry list of problems), post it and you'll likely receive help.

As for what you're trying to accomplish: is this your code? The errors you're getting (and the fact that you haven't figured some of them out), along with the fact that you placed this logic in a very strange place, makes me think that this is a copy/paste job. If not, I apologize for the assumption. The problem with that is that if you don't understand what your copy/pasted code is doing, you're going to be hard pressed to use it correctly. Again, I know I'm making assumptions, so if I'm off base I apologize.

Think about the events that you're trying to use. onselectedIndexChanged is where you handle the logic for changing the selected item in the grid view. Not the place to control the behavior when it loads. So move all code that's trying to control the displayed data to OnRowDataBound. Next, think about how you're going to control what the user sees, based on roles. You can handle it several ways. You can do what you're trying to do, and manipulate the visibility of the columns based on those roles. You could also choose what data to bind to the GridView based on the roles (that logic would obviously have to be in whatever method that binds the data), or you could even have different queries based on the roles (that's assuming that non-admin users won't need the data that you're hiding). But before you start trying to code whatever approach you use, you need to plan how it's all going to work. Think through the process of loading the page, retrieving the data/creating your data source, and binding it to the gridview, and displaying it. There are many points in that process where you can break in and make your changes, but you need to figure out the correct place based on the method you're using.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1