Click here to Skip to main content
15,885,686 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello

How can I Remove or hide the column Auto Genrated Fields of Gridview at Run Time?
now in current code the output (Runtime) Gridview shows the same column fields as stored in database. I want to display selected fields.

Code:

protected void Button1_Click(object sender, EventArgs e)
  {
      GridView1.Visible = true;
      SqlCommand cmd = new SqlCommand();
      cmd.Parameters.Add("@ten", SqlDbType.Float).Value = TextBox1.Text;
      cmd.Parameters.Add("@twel", SqlDbType.Float).Value = TextBox2.Text;
      cmd.CommandText = "select * from table1 where (ten >=  @ten) and (twel >= @twel)";
      cmd.Connection = con;
      SqlDataReader dr;
      dr = cmd.ExecuteReader();
      if (dr.HasRows)
      {
          dr.Read();
          GridView1.DataSource = dr;
          GridView1.DataBind();
                         // To edit Header Text of gridview at run time
          GridViewRow header = GridView1.HeaderRow;
          header.Cells[0].Text = "10th %age";
          header.Cells[1].Text = "12th %age";
      }
      GridView1.Columns.RemoveAt(1);
  }


When I use GridView1.Columns.RemoveAt(1);
for removing second column then ERROR is:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

in this code out put is shows the both 2 fields. and required is only first field.
If there is an another method then please suggest me...

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 1-Mar-11 23:54pm
v3

Firstly, are you sure there are any table rows which meet your conditions? If there aren't, then
C#
if (dr.HasRows)
will fail, and you will not bind any data to the GridView.
This would cause the error you describe!

Try moving
MIDL
GridView1.Columns.RemoveAt(1);
up into the block where you bind the data...

But why are you getting data from the database if you don't want it? It is a lot more efficient to change your SELECT statement:
SQL
cmd.CommandText = "select * from table1 where (ten >=  @ten) and (twel >= @twel)";
To
SQL
cmd.CommandText = "select ten from table1 where (ten >=  @ten) and (twel >= @twel)";
and not to return the content of column one at all.

It is generally considered bad practice to use "SELECT * FROM" when you know what the fields you are interested in are...
 
Share this answer
 
Comments
Albin Abel 2-Mar-11 6:07am    
My 5
Shagun Bansal 2-Mar-11 6:18am    
thanks it works.
if i have 4 columns in table. (Name, Address, Age, Salary)
and i want to display 3 columns (Name, Age, Salary)

then what change is to be done in the sql query...?
because in this:
cmd.CommandText = "select ten from table1 where (ten >= @ten) and (twel >= @twel)";

it shows the result of "ten" column..
OriginalGriff 2-Mar-11 7:02am    
Predictably enough, you replace "ten" with the list of columns you want to display...
cmd.CommandText = "SELECT Name, Age, Salary FROM table1 WHERE (ten >= @ten) AND (twel >= @twel)";
The optimal method would be selecting required columns. Why select all columns and hide after that, though ways are there to hide.
 
Share this answer
 
v2
Suppose you have 3 columns in a gridview.
"ID", "Name", "Age"
Index value of these columns would be 0, 1, 2

so if you wanna remove the column "Name", you'd use.
GridView1.Columns.RemoveAt(1);
After you do that The Grid will re-arrange its columns. It will have 2 columns then.
"ID", "Age".
So technically this code won't work.

GridView1.Columns.RemoveAt(1);
GridView1.Columns.RemoveAt(2);
Index will be out of range. Cause after first line of code it has re-arranged the columns with the indexes.

MIDL
GridView1.Columns.RemoveAt(1);
GridView1.Columns.RemoveAt(1);

But this will remove both "Name" and "Age" columns.

While you're using loop, remember to do i-- when the RemoveAt() condition meets.
 
Share this answer
 
v2

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