HI Try like this..
Consider your
Gridview
has
two bound field columns as below
set the
AutoGenerateColumns
as
false
<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:
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
List<DataControlField> lstStaticColumns = new List<DataControlField>();
foreach (DataControlField col in GridView1.Columns)
lstStaticColumns.Add(col);
Clear the GridView Columns:
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 )
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 )
foreach (DataControlField col in lstStaticColumns)
GridView1.Columns.Add(col);
Bind the GridView as below... :)
GridView1.DataSource = dt;
GridView1.DataBind();
Note: Tested Working Fine..