Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I am using gridView. I have some auto generated columns and some generated by me. Now the column which I have generated is displayed first and then the auto generated columns. I want to display auto generated columns first and then my generated columns.

IMP Note 1 :- I can't use this (for example): -

C#
<asp:BoundField HeaderText="My First Column" DataField="myField1" />

Note 2 : No. of columns and name of columns are dynamic
Posted
Updated 16-Jan-14 2:43am
v3
Comments
Karthik_Mahalingam 16-Jan-14 5:06am    
what you have tried???

Try This

datagridview.Columns["ColumnName"].DisplayIndex = 1;//Replace 1 with your index where you want the column to be displayed.
 
Share this answer
 
v3
Comments
souvikcode 22-Jan-15 6:44am    
asp gridview has display index?
change gridview templatecolumn order dynamically.
1.Iterate through all coloumns of the GridView object and Store them in a collection.
XML
List<DataControlField> coloumns = new List<DataControlField>();
foreach (DataControlField coloumn in gv.Columns)
{
    coloumns.Add(coloumn);
}

2.Rearrange the coloumn-objects as you want in the collection.
//Rearrange coloumns' collection..

3.Clear all coloumns of the GridView object and add coloumns from the collection to GridView object.
C#
gv.Columns.Clear();
 foreach (DataControlField coloumn in coloumns)
 {
     gv.Columns.Add(coloumn);
 }
 
Share this answer
 
HI Try like this..

Consider your Gridview has two bound field columns as below
set the AutoGenerateColumns as false

ASP.NET
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
       <Columns>
           <asp:BoundField HeaderText="____Static_Col_AA" DataField="AutoColum_AA" ItemStyle-Width="50px" />
           <asp:BoundField HeaderText="____Static_Col_BB" DataField="AutoColum_BB" ItemStyle-Width="50px" />
       </Columns>
   </asp:GridView>


assume this sample datasource:

C#
DataTable dt = new DataTable();
               dt.Columns.Add("AutoColum_AA", typeof(int));
               dt.Columns.Add("AutoColum_BB", typeof(int));
               dt.Columns.Add("AutoColum_CC", typeof(int));
               dt.Columns.Add("AutoColum_DD", typeof(int));

               for (int i = 0; i < 6; i++)
                   dt.Rows.Add(i, i, i, i);



Store all the static columns of the gridview to a list as

C#
List<DataControlField> lstStaticColumns = new List<DataControlField>();
               foreach (DataControlField col in GridView1.Columns)
                   lstStaticColumns.Add(col);


Clear the GridView Columns:
C#
GridView1.Columns.Clear();


Create the Dynamic Columns in the codebehind as
( this will be auto generated columns which will be appearing first in the gird view )
C#
foreach (DataColumn col in dt.Columns)
                {
                    BoundField bfield = new BoundField();
                    bfield.DataField = col.ColumnName;
                    bfield.HeaderText = col.ColumnName;
                    GridView1.Columns.Add(bfield);
                }


Add the Static Columns to the GridView ( bound (static) columns will be appearing at the end )

C#
foreach (DataControlField col in lstStaticColumns)
                   GridView1.Columns.Add(col);


Bind the GridView as below... :)

C#
GridView1.DataSource = dt;
               GridView1.DataBind();


Note: Tested Working Fine..
 
Share this answer
 
Comments
Member 10549697 26-Jul-17 3:19am    
Hi Karthik, This is a good example. actually this is what i need. but it is binding those columns twice. why?
Member 10549697 26-Jul-17 3:34am    
Sorry. i have to make autogeneratedcolumns=false.
Karthik_Mahalingam 26-Jul-17 3:39am    
cool
If you are adding columns in a DataTable in Code Behind , You can use SetOrdinal(Index) method of DataTable to set the order of columns.

C#
DataTable tolfree = ds.Tables[0];

tolfree.Columns.Add(new DataColumn { ColumnName = "CTCHandled", DataType = typeof(int), DefaultValue = 0 });
tolfree.Columns.Add(new DataColumn { ColumnName = "TotalHandled", DataType = typeof(int), DefaultValue = 0 });

tolfree.Columns["Agent_Id"].SetOrdinal(0);      //Auto Generated Column
tolfree.Columns["Agent_Name"].SetOrdinal(1);    //Auto Generated Column
tolfree.Columns["CTCHandled"].SetOrdinal(2);              //Manually Generated column
tolfree.Columns["TotalHandled"].SetOrdinal(3);            //Manually Generated column
 
Share this answer
 
v2
Thanks for your replies. I tried all the above solutions, but they are not worked.
I resolved this by using Jquery as bellow,

JavaScript
$(document).ready(function() {
    var ColCount = $("#gvEvalRankReport tr:nth-child(2) td").length
    jQuery.each($("#gvEvalRankReport tr"), function() {
    $(this).children(":eq(" + (ColCount - 1) + ")").after($(this).children(":eq(1)"));
    $(this).children(":eq(" + (ColCount - 1) + ")").after($(this).children(":eq(0)"));
    $(this).children(":eq(" + (ColCount - 3) + ")").hide();
    });
});
 
Share this answer
 
You could change the input SQL Statement:
SQL
    cmdStr = "SELECT * FROM table1;"
To
    cmdStr = "SELECT * FROM table1 ORDER BY col1 ASC;"
Or
    cmdStr = "SELECT * FROM table1 ORDER BY col1 DESC;"
 
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