Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GridView Column Remove Errorin asp.net 2.0 using C#

Posted on 2009-05-04
9
Medium Priority
?
660 Views
Last Modified: 2012-05-06
Hi All

I tried to bind some values to the gridview from the database and it uses a primary key to join another table.

What i need is i donn wann to display the primary key in the gridview.

I have tried various properties for the gridviews like

GridView1.Columns.RemoveAt(0);

GridView1.Columns[0].Visible = false ;

Finally i got the

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
protected void btnSearchMain_Click(object sender, EventArgs e)
    {
        
        if (rbtnCollege.Checked == true)
        {
            SqlDataAdapter da = new SqlDataAdapter("select EDU_College.CId as 'College Number',EDU_College.CName as 'College Name',EDU_College.CAddressLine1 As 'College Address',EDU_City.City from EDU_College join EDU_City on EDU_City.CityId=EDU_College.CF_CityId where EDU_College.CName like '%" + txtSearchMain.Text + "%'", Conn);            
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
           //GridView1.Columns.RemoveAt(1);
            //GridView1.Columns.RemoveAt(GetIndex(GridView1, "CId"));
            GridView1.Columns[1].Visible = false;
            GridView1.DataBind();
            GridView1.Columns[1].Visible = false;
        }
        else
        {
            SqlDataAdapter da = new SqlDataAdapter("select EDU_College.CId as 'College Number',EDU_College.CName as 'College Name',EDU_College.CAddressLine1 As 'College Address',EDU_City.City as City from EDU_College join EDU_City on EDU_City.CityId=EDU_College.CF_CityId where EDU_City.City like '%" + txtSearchMain.Text + "%'", Conn);            
            DataTable dt = new DataTable();            
            da.Fill(dt);                        
            GridView1.DataSource = dt;
            //GridView1.Columns.RemoveAt(1);
            GridView1.DataBind();
 
        }
    }

Open in new window

0
Comment
Question by:kamikazis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 24302080
Try set the visibility to false after binding the gridview and not before
0
 

Author Comment

by:kamikazis
ID: 24302123
have tried that too and not mentioned above.

i got the same error :  Index was out of range. Must be non-negative and less than the size of the collection.

0
 

Author Comment

by:kamikazis
ID: 24302141
forgot to comment the code
            // GridView1.Columns[1].Visible = false;
            GridView1.DataBind();
            GridView1.Columns[1].Visible = false;

Same error
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Expert Comment

by:informaniac
ID: 24302172
You'll get the error if u have set the gridview autogeneratecolumns = true

Try setting the Datakey property of the gridview to the primary key column of datatable.
0
 

Author Comment

by:kamikazis
ID: 24302278
if i set the autogeneratecolumns = false

Nothing Displayed in the gridview.
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24302337
Hi,

Do a test before you remove the Column. E.g if GridView1.Columns.Count() > 0 then remove it. Atleast that will narrow down the causes of the problem if it does return a count of 0.

HTH
-M3mph15
0
 
LVL 20

Accepted Solution

by:
informaniac earned 200 total points
ID: 24302578
The feature of gridview says that you when u have set Autogeneratecolumns=true GridView1.Columns.Count() will give u nothing....

When u r setting autogeneratecolumns = false

then u have to manually add columns to ur gridview..

http://authors.aspalliance.com/aspxtreme/webforms/controls/addingboundfieldstogridview.aspx
0
 

Author Closing Comment

by:kamikazis
ID: 31577903
Thanks

Regards
Kamikazis
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question