views:

901

answers:

6

when I dispaly the rows from DB using GridView, the columns appear without order. like:

Class Course Name ID

and I want to display them by order. like:

ID Name Class Course

how can I do that ?

this is thr first piont

the second point is how can I RENAME the column ?

thanks,

+1  A: 

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>
Andrew Hare
Dang! Beat me by 9 seconds! :)
n8wrl
Thank you, it's OK !
Did my answer solve your problem?
Andrew Hare
+1  A: 

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

n8wrl
thank you .
A: 

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

ex:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False">
<Columns>
     <asp:BoundColumn DataField="ID" HeaderText="ID" />
.
.
.
Josh E
thank you .
A: 

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.

Eric
thank you .
A: 
JBrooks
A: 

How to reorder columns in a datagrid dynamically?

Som