简体   繁体   中英

How do I join an unbound Data Column at the end of the bound DataColumns in DataGridView?

I have a DataGridView where I want to join an unbound Data Column, named Total Price .

I can add the unbound Column using the DataGridView Designer, however, when I load the bound Columns into the DataGridView, the Total Price column is at the left-most of the grid.

I want it to be at the right-most side of the grid.
I can't find any way to format the unbound Column in Visual Studio.

Are there ways to code the position of the column or does the Designer have a built-in function to move a Column's position?

The position of a Column in a DataGridView can be determined in different ways:

  1. Setting the desired order in the designer.
  2. Setting the AllowUserToOrderColumns property to true , which allows to reorder the Columns by dragging them with the Mouse.
  3. Adding the Columns in code, in a specific sequence.
  4. Setting a Column's DisplayIndex property, to modify the position of a Column in the grid at run-time.
    To note: changing the DisplayIndex property modifies the position in the grid, it does not change the Column's Index .

Move an existing Column to the last position in the grid:
(the first Column will become the last one)

dataGridView1.Columns[0].DisplayIndex = dataGridView1.Columns.Count - 1

As note before, Column[0] , even if shown in the last position, will still have index = 0 , so, still Column[0] .

Add a new Column in code and set its position in the grid:
(the new TextBoxColumn has a name, a Column Header text and occupies the first position in the grid layout)

dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() {
    Name = "NewColumn",
    HeaderText = "New Column",
    DisplayIndex = 0,
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM