Mohemmad K
Mohemmad K

Reputation: 839

How to change the order of columns in GridView using Asp.Net C#

I have a grid which is being bound in code behind in which i want to display template field also.

I am generating 3 columns in DataTable for grid view and the template field is TextBox control.

My code for binding data is..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class gr4 : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
    cn = new SqlConnection("Data Source=AMIR-PC\\MOHEMMAD;Initial Catalog=CRM_InvestPlus;Integrated Security=True");
    string query = "Select Capacity from Dealer_License_Capacity where ID='D00001' and Software_ID='001' and Version_ID='1'";
    cn.Open();
    cmd = new SqlCommand(query,cn);
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    cn.Close();

    DataTable dt = new DataTable();
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Software_Name", typeof(string));
    dt.Columns.Add("Version_Name", typeof(string));

    int count = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());

    for (int i = 0; i < count; i++)
    {
        DataRow dr = dt.NewRow();
        dr["Name"] = "aaa";
        dr["Software_Name"] = "bbb";
        dr["Version_Name"] = "ccc";

        dt.Rows.Add(dr);
    }

    GridView1.DataSource = dt;
    GridView1.DataBind();
   }
}

My source code for grid view is:

    <asp:GridView ID="GridView1" runat="server">
    <Columns>

        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
    </asp:GridView>

</div>
</form>

The grid display template field as first column but I want to display the template field as last column in output. Can I add more template fields in this grid..??

Please help..

Thanks in advance

Upvotes: 4

Views: 5239

Answers (2)

bindu
bindu

Reputation: 1

if all ready data is there in database and column is there at the same way in grid-view column name is name there has to display name is order how me not getting

my grid code is this one but not coming order wise

Upvotes: -2

शेखर
शेखर

Reputation: 17614

You can use bound fields like below

  <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false" > /*changed to false*/
     <Columns>
       <asp:BoundField HeaderText="Name" 
           DataField="Name"/>
       <asp:BoundField HeaderText="SoftwareName" 
           DataField="Software_Name"/>
       <asp:BoundField HeaderText="VersionName" 
           DataField="Version_Name"/>

       <asp:TemplateField>
           <ItemTemplate>
               <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
           </ItemTemplate>
       </asp:TemplateField>
    </Columns>
</asp:GridView>

You can change the order as you like.

Upvotes: 3

Related Questions