Identifier on grid lines

Asked

Viewed 27 times

0

They could tell me if there is any simple property or way to put an identifier in the grid lines but without being the ID in the table, for example I have 5 records with id 32,45,99,100e and 200. But I want to put on grid 1,2,3,4 and 5. Like an auto increment, some suggestion ?

Code used for Grid:

    var lista = from substituidos in vagaDTO.Substituidos
                        where substituidos.Operacao != SysDTO.Operacoes.Exclusao
                        select new
                        {            

                            substituidos.IdClasse,
                            substituidos.Id,
                            Matrícula = substituidos.Matricula_Substituido ,
                            Substituído = substituidos.Substituido,
                            Contratante = substituidos.Numero_CTT,
                            Local = (substituidos.Endereco == null ? "" : substituidos.Endereco.Sigla),
                            Dt_Enc = string.Format("{0:dd/MM/yy}", substituidos.Data_Enc),
                            Dt_Saída = string.Format("{0:dd/MM/yy}", substituidos.Data_Saida),
                            Prev_Deslig = string.Format("{0:dd/MM/yy}", substituidos.Prev_Desligamento),
                            Prev_Fer_Ini = string.Format("{0:dd/MM/yy}", substituidos.Prev_Ferias_Inicio),
                            Prev_Fer_Fim = string.Format("{0:dd/MM/yy}", substituidos.Prev_Ferias_Final),
                            Fer_Ini = string.Format("{0:dd/MM/yy}", substituidos.Ferias_Inicio),
                            Fer_Fim = string.Format("{0:dd/MM/yy}", substituidos.Ferias_Final)

                        };

            //dtgSubstituidos.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            lista = lista.OrderBy(p => p.Substituído).ToList();
            dtgSubstituidos.DataSource = lista.ToList();



            dtgSubstituidos.Columns[0].Visible = false;
            dtgSubstituidos.Columns[1].Visible = false;
            dtgSubstituidos.Columns[4].Visible = false;

            dtgSubstituidos.Columns[2].Width = 50;
            dtgSubstituidos.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            dtgSubstituidos.Columns[3].Width = 150;
            dtgSubstituidos.Columns[5].Width = 200;

            dtgSubstituidos.Columns[6].Width = 60;
            dtgSubstituidos.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dtgSubstituidos.Columns[7].Width = 60;
            dtgSubstituidos.Columns[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dtgSubstituidos.Columns[8].Width = 60;
            dtgSubstituidos.Columns[8].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dtgSubstituidos.Columns[9].Width = 60;
            dtgSubstituidos.Columns[9].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dtgSubstituidos.Columns[10].Width = 60;
            dtgSubstituidos.Columns[10].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dtgSubstituidos.Columns[11].Width = 60;
            dtgSubstituidos.Columns[11].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dtgSubstituidos.Columns[12].Width = 60;
            dtgSubstituidos.Columns[12].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;                            
  • If you want to know which line you selected, just use grid.CurrentRow, so you don’t even need an identifier

1 answer

0


declares a int outside the expression, and place a property with the value within the expression, and increase:

 int i  = 0;
 var lista = from substituidos in vagaDTO.Substituidos
                    where substituidos.Operacao != SysDTO.Operacoes.Exclusao
                    select new
                    {            
                        Sequencia = i++;
                        substituidos.IdClasse,
                        substituidos.Id,
                        Matrícula = substituidos.Matricula_Substituido ,
                        Substituído = substituidos.Substituido,
                        Contratante = substituidos.Numero_CTT,
                        Local = (substituidos.Endereco == null ? "" : substituidos.Endereco.Sigla),
                        Dt_Enc = string.Format("{0:dd/MM/yy}", substituidos.Data_Enc),
                        Dt_Saída = string.Format("{0:dd/MM/yy}", substituidos.Data_Saida),
                        Prev_Deslig = string.Format("{0:dd/MM/yy}", substituidos.Prev_Desligamento),
                        Prev_Fer_Ini = string.Format("{0:dd/MM/yy}", substituidos.Prev_Ferias_Inicio),
                        Prev_Fer_Fim = string.Format("{0:dd/MM/yy}", substituidos.Prev_Ferias_Final),
                        Fer_Ini = string.Format("{0:dd/MM/yy}", substituidos.Ferias_Inicio),
                        Fer_Fim = string.Format("{0:dd/MM/yy}", substituidos.Ferias_Final)

                    };

with this, your Grid column 0 will be the Sequencia.

Attention: your ordering should be done also in LINQ, otherwise nothing will help the numbering.

source: https://stackoverflow.com/a/34411972/4713574

Browser other questions tagged

You are not signed in. Login or sign up in order to post.