When you associate a list <> with a datagrid - how to change column names?

I have a code:

VideoChannel[] channels = GetVideoChannels();

dataGridView1.DataSource = channels;
dataGridView1.Refresh();

VideoChannel is a class with many properties. This code works fine, but I want to change the column names. By default, column name = VideoChannel property name. Is there any attribute that I can mark the VideoChannel property, so the column name! = Property name?

+3
source share
3 answers

You can try DisplayNameAttribute. Decorate your class with a property.

 public class SomeItem
    {
        [DisplayName("SomeItem")]
        public string Name { get; set; }
    }
+8
source

You can change the name of a column in design mode, where you create a column and set its properties. Or you can try

DataGridName.Colimns[0].HeaderText = "Your Header0";
DataGridName.Colimns[1].HeaderText = "Your Header1";
.
.
.
DataGridName.Colimns[N].HeaderText = "Your HeaderN";

- .

+1

dataGridView1.TableStyles[0].GridColumnStyles[0].HeaderText = "SomeDifferentColumnName"

dataGridView1.Columns[0].HeaderText = "SomeDifferentColumnName"

Source: DataGridView Edit Column Names

Source in turn: http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/8b9b07d4-06fc-4c12-9509-0c19ca04e003/

0
source

Source: https://habr.com/ru/post/1747491/


All Articles