Tutorial :How can I reorder the columns in GridView and Rename them?


When I display the rows from DB using GridView, the columns appear without order. Like:

Class Course Name ID

and I want to display them in order like:

ID Name Class Course

How can I do that ?

This is the first piont.

The second point is how can I RENAME a column ?



the simplest solution is NOT to use


instead specify your columns in the order you want.

SELECT ID, Name, Class, Course FROM....

as far as Renaming your column, that can be accomplished in SQL as well.

SELECT ID as A, Name as B, Class as C, Course as D FROM....


You need to explicitly create the columns yourself rather then allow the GridView to generate them for you. If you do this you will be able to order them however you like and rename them as well.

Here is an example (the headertext property will allow you to rename the column):

<asp:gridview id="foo" runat="server" autogeneratecolumns="false">      <columns>          <asp:boundfield datafield="ID" headertext="Identifier" />          <asp:boundfield datafield="Name" />          <asp:boundfield datafield="Class" />          <asp:boundfield datafield="Course" />      </columns>   </asp:gridview>  


Turn off 'autogenerate columns' and specify the columns in whatever order/formatting you want.


First set AutoGenerateColumns to FALSE in your markup. Then you can add columns and order them explicitly.


<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False">  <Columns>       <asp:BoundColumn DataField="ID" HeaderText="ID" />  .  .  .   


If you are connecting to a datasource you just have to click the gridview's arrow on the form and then Edit columns and you can order them by clicking the up or down arrow and to the right of those options you will see boundfield properties. You can change the HeaderTExt to whatever you want it to be.


In a few cases you can't set AutoGenerateColumns to false.

I have an application (ReleaseManager) where I don't know the SQL in advance. So during the setup of the site they enter a connection string and the SQL to list their bugs from their bug tracking database. I use this to get a table at runtime with the rows from their bug tracker. From there I add a column of check boxes so they can select bug tracking items to go with the release request.

dtIssues.Columns.Add(new DataColumn("Include", typeof(Boolean)));  dtIssues.Columns["Include"].SetOrdinal(0);  

So I added a Boolean column which binds as a check box and then I set it to be the first column. My GridView has AutoGenerateColumns set to "True".

The requirment for the SQL they entered is that the first column has to be the Id column on their bug tracking table (the rest I don't care about), I can rename what they give me like:

dtIssues.Columns[1].ColumnName = "ID";  

So this is what it looks like:

alt text http://www.digitaltools.com/Images/Issues.png

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »