Gridview, change dynamically headerText

user2265252 picture user2265252 · Jun 25, 2013 · Viewed 19k times · Source

I have a gridview and I want to change dynamically the name of header. It is possible ? I have this code :

OracleCommand cmdReqStockComp = new OracleCommand(reqStockCompTotal);
cmdReqStockComp.Connection = oConnexion;
OracleDataReader readerReqStockComp = cmdReqStockComp.ExecuteReader();

// ************** ETAPE 2 : On remplit la GridView ************ //

// On lie le résultat de la requête à la GridView
gvReportingStockComp.DataSource = readerReqStockComp;
gvReportingStockComp.DataBind();

And this aspx code :

<asp:GridView ID="gvReportingStockComp" runat="server" AutoGenerateColumns="false" Visible="false">

            <Columns>

                <asp:BoundField DataField="cod_wo" HeaderText="N° OF" />
                <asp:BoundField DataField="composant" HeaderText="Composant" />
                <asp:BoundField DataField="BESOIN" HeaderText="Besoin/OF" />
                <asp:BoundField DataField="BESOIN_T" HeaderText="Besoin total" />
                <asp:BoundField DataField="stock_dispo" HeaderText="Stock dispo" />
                <asp:BoundField DataField="QTE_RESTANTE" HeaderText="Qte restante" />

            </Columns>

        </asp:GridView>

Thanks :)

Answer

Andrei picture Andrei · Jun 25, 2013

On a very basic level you can do just

gvReportingStockComp.Columns[0].HeaderText = "New Header for First Column";