Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
i am using asp.net 4.0
1. I want to remove come columns of data base form Auto genrate fields in Gridview.
2. how can i change th Header Text of Auto genrated columns from Gridview at run time
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();
    }

}


[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 1-Mar-11 20:53pm
v2

Don't post the same question twice: it has been less than 5 minutes, give people a chance to answer, rather than being so impatient!
 
Share this answer
 
Please don't delete your questions, when they have been answered! It is very rude...

Anyway, for others, here is the solution again:

Use the GridView.HeaderRow property:

C#
GridViewRow header = gridViewPagedLog.HeaderRow;
header.Cells[0].Text = "Hello";
header.Cells[1].Text = "There";
header.Cells[2].Text = "Mush";
 
Share this answer
 
Comments
Shagun Bansal 2-Mar-11 3:26am    
Thankyou sir..

when i run with this code:
GridView1.DataSource = dr;
GridViewRow header = GridView1.HeaderRow;
header.Cells[0].Text = "hello";
header.Cells[1].Text = "there";
GridView1.DataBind();


/* Object reference not set to an instance of an object.
ERROR COMES.. PLEASE HELP ME */
OriginalGriff 2-Mar-11 3:56am    
Perhaps if you move the header stuff to after the DataBind, when the columns have a chance of having been created...
Shagun Bansal 2-Mar-11 4:52am    
Thank you very much sir for your support. it WORKS

Now i am trying to remove or hide some columns at run time.
the current output is same as database fields.

when i try:
GridView1.Columns.RemoveAt(1);
/* ERROR is:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index */
OriginalGriff 2-Mar-11 5:00am    
Ask this as a separate question, and include a code fragment so we can see the order that you do things: it makes life easier for us all.

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