locked
C# GridView Column Order RRS feed

  • Question

  • User1501155440 posted

    I am manually adding columns to an empty GridView and then databinding to it.

    I add the columns in order, but when I databind, the GridView shows the columns out of order.  How can I rearrange or set the columns?

    I add the columns:

    DataTable peopleCourses = new DataTable();
    peopleCourses.Columns.Add(new DataColumn("Id", typeof(int)));
    peopleCourses.Columns.Add(new DataColumn("Name", typeof(string)));
    peopleCourses.Columns.Add(new DataColumn("Position", typeof(string)));

    // then a bunch of code to actually add the data rows to peopleCourses.

    // then I databind

    GridView1.DataSourceID = null;
    GridView1.BackColor = System.Drawing.Color.White;
    GridView1.DataSource = peopleCourses;
    GridView1.DataBind();

    Instead of Id, Name, Position the GridView shows Position, Id, Name.  What can I do to make the GridView show in the order of Id, Name, Position?

    Any help would be greatly appreciated.

    Thanks,
    Jason

    Thursday, August 30, 2018 10:56 PM

Answers

  • User632428103 posted

    re;

    if i copy all your code and paste in a solution of test and load the page i obtain this order :

    Employee - Position - test - test2 - test3

    i can't understand , try to load the page WITHOUT the datas ...and see when the gridview is empty if the order are correct ...

    after, check perhpas your query ...but i can't understand really ...sorry !

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 3, 2018 12:24 PM

All replies

  • User632428103 posted

    Hello,

    check PERHPAS your query but that's strange if i do this i obtain well the correct order :

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bindGridView();
                }
            }
    
            void bindGridView()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Id", typeof(int)));
                dt.Columns.Add(new DataColumn("Name", typeof(string)));
                dt.Columns.Add(new DataColumn("Postion", typeof(string)));
    
                //
                for (int i = 0; i < 10; i++)
                {
                    dt.Rows.Add(i, "Name", "Position");
                }
    
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

    Friday, August 31, 2018 6:43 AM
  • User1501155440 posted

    Thank you, Jimmy.  Thank you for your suggestion.  The results are the same.  When I test with just 3 fields, I see that the fields are displaying in opposite order of the load order.

    For example, I load the datatable in the order of id, name, position.  But they are displayed the opposite such as position, name, id.  Very strange.

    It would seem that the columns should display in the build order.  Or, at the very least, I should be able to rearrange the gridview columns.

    Any further suggestions would be greatly appreciated.

    Jason

    Friday, August 31, 2018 3:51 PM
  • User-893317190 posted

    Hi Jason Duncan,

    Are you sure you that your gridview is empty?

    Could you show how you design your aspx page and how you add data to your datatable?

    If just an empty gridview , the order will be the same as you add your columns to your datatable .

    Please post more of your code.

    Best regards,

    Ackerly Xu

    Monday, September 3, 2018 6:28 AM
  • User632428103 posted

    Hello all,

    @jason => could you post all the code with query ?

    thanks

    Monday, September 3, 2018 6:32 AM
  • User1501155440 posted

    Thank you very much for your assistance.  I've been stuck for several days, and just cannot figure it out.  Very appreciative of the help.

    To simplify, I have stripped the code down as simple as I can. As you can see in the screenshot the fields are not in order that I create them.  There does not seem to be any pattern to how they are being displayed in the gridview.  I need the fields in the order that I am creating them.

    DataTable peopleCourses = new DataTable();

    peopleCourses.Columns.Add(new DataColumn("PeopleName", typeof(string)));
    peopleCourses.Columns.Add(new DataColumn("Position", typeof(string)));
    peopleCourses.Columns.Add(new DataColumn("Test", typeof(string)));
    peopleCourses.Columns.Add(new DataColumn("Test2", typeof(string)));
    peopleCourses.Columns.Add(new DataColumn("Test3", typeof(string)));

    peopleCourses.Rows.Add("jason","Mechanic","course1", "course2", "course3");


    GridView1.DataSource = null;
    GridView1.DataSourceID = null;


    BoundField bfield;


    bfield = new BoundField();
    bfield.DataField = "PeopleName";
    bfield.HeaderText = "Employee";
    bfield.HeaderStyle.ForeColor = System.Drawing.Color.White;
    bfield.HeaderStyle.BackColor = System.Drawing.Color.SteelBlue;
    bfield.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.HeaderStyle.Width = Unit.Pixel(150);
    GridView1.Columns.Add(bfield);

    bfield = new BoundField();
    bfield.DataField = "Position";
    bfield.HeaderText = "Position";
    bfield.HeaderStyle.ForeColor = System.Drawing.Color.White;
    bfield.HeaderStyle.BackColor = System.Drawing.Color.SteelBlue;
    bfield.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.HeaderStyle.Width = Unit.Pixel(150);
    GridView1.Columns.Add(bfield);

    bfield = new BoundField();
    bfield.DataField = "Test";
    bfield.HeaderText = "Test";
    bfield.HeaderStyle.ForeColor = System.Drawing.Color.White;
    bfield.HeaderStyle.BackColor = System.Drawing.Color.SteelBlue;
    bfield.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.HeaderStyle.Width = Unit.Pixel(150);
    GridView1.Columns.Add(bfield);

    bfield = new BoundField();
    bfield.DataField = "Test2";
    bfield.HeaderText = "Test2";
    bfield.HeaderStyle.ForeColor = System.Drawing.Color.White;
    bfield.HeaderStyle.BackColor = System.Drawing.Color.SteelBlue;
    bfield.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.HeaderStyle.Width = Unit.Pixel(150);
    GridView1.Columns.Add(bfield);

    bfield = new BoundField();
    bfield.DataField = "Test3";
    bfield.HeaderText = "Test3";
    bfield.HeaderStyle.ForeColor = System.Drawing.Color.White;
    bfield.HeaderStyle.BackColor = System.Drawing.Color.SteelBlue;
    bfield.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    bfield.HeaderStyle.Width = Unit.Pixel(150);
    GridView1.Columns.Add(bfield);

    GridView1.DataSource = null;

    GridView1.DataSource = peopleCourses;
    GridView1.DataBind();

    Screenshot

    Monday, September 3, 2018 10:26 AM
  • User1501155440 posted

    Here is the screenshot:

    And on page load, I am sure to set AutoGenerate to false.

    GridView1.AutoGenerateColumns = false;

    Monday, September 3, 2018 10:29 AM
  • User1501155440 posted

    Thank you.  Yes, the gridview is empty.

    My page is quite simple at this stage.  On page load it calls a function BindIt().  Which builds and loads the GridView.

    Monday, September 3, 2018 10:37 AM
  • User632428103 posted

    re;

    if i copy all your code and paste in a solution of test and load the page i obtain this order :

    Employee - Position - test - test2 - test3

    i can't understand , try to load the page WITHOUT the datas ...and see when the gridview is empty if the order are correct ...

    after, check perhpas your query ...but i can't understand really ...sorry !

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 3, 2018 12:24 PM
  • User1501155440 posted

    Thank you, Jimmy69.  I took your advice and moved the code to it's own separate page with just an empty grid with no OnRowCreated or OnRowDataBount.  When I did that, the order is correct.  When I went back and scoured my previous code, there was a snippet of code in the OnRowCreated that was adding an edit button so that was the culprit.  Needless to say, this was quite painful to find as I had completely overlooked that something in RowCreated would effect the order.

    Much appreciated, and this is now resolved.

    Jason

    Monday, September 3, 2018 4:39 PM