Add row in a dataTable

4

I need to add a row in a table in a given position.

I have the following methods:

    public void criarTabela(DateTime dia)
    {
        tabela = new DataTable();

        switch (dia.DayOfWeek)
        {                
            //case DayOfWeek.Monday: dia.AddDays(-1); break;
            case DayOfWeek.Tuesday: dia = dia.AddDays(-1); break;
            case DayOfWeek.Wednesday: dia = dia.AddDays(-2); break;
            case DayOfWeek.Thursday: dia = dia.AddDays(-3); break;
            case DayOfWeek.Friday: dia = dia.AddDays(-4); break;
            case DayOfWeek.Saturday: dia = dia.AddDays(-5); break;
            case DayOfWeek.Sunday: dia = dia.AddDays(-6); break;                  
        }
        tabela.Columns.Add("Atendimento");
        tabela.Columns.Add("Hora");
        string diahoje = String.Format("{0:ddd, MMM d, yyyy}", dia);
        tabela.Columns.Add("" + diahoje);

        for (int data = 1; data < 7; data++)
        {
            dia = dia.AddDays(1);
            diahoje = String.Format("{0:ddd, MMM d, yyyy}", dia);
            tabela.Columns.Add("" + diahoje);
        }

        DataRow l = tabela.NewRow();
        TimeSpan horaI = new TimeSpan(10, 00, 00);
        TimeSpan horaF = new TimeSpan(22, 00, 00);              
        for (TimeSpan horaS = horaI; horaS <= horaF; )
        {
            l = tabela.NewRow();
            var horaFormatada = string.Format("{0:hh\:mm}", horaS);
            l["Hora"] = horaFormatada;
            tabela.Rows.Add(l);
            var trintaMin = new TimeSpan(0, 30, 0);
            horaS = horaS.Add(trintaMin);
        }
    }


    public void carregarAgenda()
    {
        criarTabela(diaAgenda);

        AgendaBLL agenda = new AgendaBLL();
        AgendaModel objAgenda = new AgendaModel();

        diaInicioAgenda = DateTime.ParseExact(tabela.Columns[2].ToString(), "ddd, MMM d, yyyy", new CultureInfo("pt-BR"));
        diaFimAgenda = DateTime.ParseExact(tabela.Columns[8].ToString(), "ddd, MMM d, yyyy", new CultureInfo("pt-BR"));

        listaAgenda = agenda.carrega(diaInicioAgenda, diaFimAgenda, Profi);                   

        foreach (DataGridViewColumn coluna in gridAgenda.Columns)
        {
            foreach (var list in listaAgenda)
            {
                int comparaData = diaInicioAgenda.CompareTo(list.Atendimento.Data_Servico);

                //Data Menor
                if (comparaData == -1)
                {
                    diaInicioAgenda = diaInicioAgenda.AddDays(1);
                }
                //Data Igual
                if (comparaData == 0)
                {
                    //diaInicioAgenda = diaInicioAgenda.AddDays(1);
                    adicionarLinha(list);
                }
                //Data Maior
                if (comparaData == 1)
                {
                    diaInicioAgenda = diaInicioAgenda.AddDays(1);
                }
            }
        }
        configuracoesGrade(); 
    }

I have the following result:

I need to create this table so that the time is Increasing how could I do this?

    
asked by anonymous 30.11.2015 / 03:03

1 answer

3

I know of two methods: 1) Sort while creating table:

tabela.Columns.Add("Hora");
var LinhasOrdenadas = from linha in dt.AsEnumerable()
                      orderby  linha.Field<DateTime>("Hora")
                      select row;
DataTable TabelaOrdenada = LinhasOrdenadas.CopyToDataTable();

The other way is using DataView

// primeiro cria-se o DataView a partir da tabela
DataView visao = new DataView(tabela);

// depois você ordena usando a coluna que você quer, pode ser mais de uma
visao.Sort = "Hora ASC";
    
30.11.2015 / 03:39