C sharp - Ordenar un data table

 
Vista:
sin imagen de perfil

Ordenar un data table

Publicado por Ruben Dario (23 intervenciones) el 27/10/2015 12:50:16
Buenos Días

Necesito ordenar un data table

tengo esto
1
2
3
4
DataView dv = dt.DefaultView;
dv.Sort = "fecha_c desc";
DataTable sortedDT = dv.ToTable();
this.dataGridView1.DataSource = sortedDT;

Pero Resulta que cunaod ordena por fecha me ordena de la sigueinte forma todos los pedidos

01/01/2015
01/01/2015
01/02/2015
01/02/2015
01/02/2015
02/01/2015
02/01/2015

Como puedo hacer para que ordene como tiene que ser por meses

Saludos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Ordenar un data table

Publicado por David (60 intervenciones) el 27/10/2015 23:19:53
Así de primeras se me ocurre que los datos que tienes en fecha_c son de tipo String, no DateTime.
Inserta los datos del tipo correcto, es decir, DateTime.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Ordenar un data table

Publicado por Ruben Dario (23 intervenciones) el 28/10/2015 12:07:13
Buenos Días Donde tengo que cambiar el formato esto tengo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private DataTable GetDataTable(){
DataTable dt= new DataTable();
//DataColumn dc = new DataColumn();
dt.Columns.Add("id");
dt.Columns.Add("pedidokey");
dt.Columns.Add("riqi");
dt.Columns.Add("fecha_c");
dt.Columns.Add("kehuID");
dt.Columns.Add("name");
dt.Columns.Add("yewuyuanID");
dt.Columns.Add("py");
dt.Columns.Add("total");
 
 
return dt;

En el load

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
private void Form1_Load(object sender, EventArgs e)
{
	//Restamos un mes a la fecha desde
	DateTime dte = DateTime.Now;
	dte = dte.AddMonths(-1);
	dateTimePicker1.Value = dte;
	//Query Pedido Cabecera
 
	string querypedido = @"Select p.id, p.pedidokey, p.riqi, p.fecha_c,  p.kehuID, c.name, p.yewuyuanID, v.py, p.jinez
				from pedidolist p, kehu c, yewuyuan v
				where p.kehuid = c.bianhao and p.yewuyuanID = v.bianhao          
			Order by p.id ASC";
 
 
	string queryweb = @"select c.pedidocodigo, c.cod_cliente, c.namecliente, c.cod_representante, c.representante, c.fecha_creacion,
						c.pedidokey, c.total, c.comentario  
						from  pedidocabeceraweb c  
						order by c.pedidocodigo ASC";
 
 
	string querypendiente = @"select c.pedidocodigo, c.cod_cliente, c.namecliente, c.cod_representante, c.representante,
							c.fecha_creacion, c.pedidokey, c.pedidokey2, c.comentario, c.total, c.enviado
							from  pedidocabecera c 
							order by c.pedidocodigo ASC";
 
	string querydipo = @"select  c.pedidocodigo, c.cod_cliente, c.namecliente, c.cod_representante, c.representante, c.fecha_creacion,
					c.pedidokey, c.comentario, c.total 
					from  pedidocabeceradipo c
					order by c.pedidocodigo ASC";
 
 
 
	MySqlDataAdapter da = new MySqlDataAdapter(querypedido,CadenaConexion);
 
	DataTable dPedidos = new DataTable();
	da.Fill(dPedidos);
 
	DataTable dt = GetDataTable();
 
	//tienes que recorrer la tabla pedidos con un bucle
	//dentro de cada bucle tienesnque obtener el resto de tablas
 
	for (int i = 0; i < dPedidos.Rows.Count; i++)  //Tabla PEDIDOS
	{
		DataRow dr = dPedidos.Rows[i];
 
		DataRow newRow = dt.NewRow();
 
 
		newRow["id"] = dr["id"];
		newRow["pedidokey"] = dr["pedidokey"];
		newRow["riqi"] = dr["riqi"];
		newRow["fecha_c"] = dr["fecha_c"];
		newRow["kehuID"] = dr["kehuID"];
		newRow["name"] = dr["name"];
		newRow["yewuyuanID"] = dr["yewuyuanID"];
		newRow["py"] = dr["py"];
		newRow["total"] = dr["jinez"];
 
		dt.Rows.Add(newRow);
	}
 
	MySqlDataAdapter daWeb = new MySqlDataAdapter(queryweb, CadenaConexionGestion);
 
	DataTable dWeb = new DataTable();
	daWeb.Fill(dWeb);
 
 
	for (int i = 0; i < dWeb.Rows.Count; i++) //Tabla WEB
	{
		DataRow drWeb = dWeb.Rows[i];
 
		DataRow newRow = dt.NewRow();
 
		newRow["id"] = drWeb["pedidocodigo"];
		newRow["pedidokey"] = drWeb["pedidokey"];
		newRow["riqi"] = drWeb["fecha_creacion"];
		newRow["fecha_c"] = drWeb["fecha_creacion"];
		newRow["kehuID"] = drWeb["cod_cliente"];
		newRow["name"] = drWeb["namecliente"];
		newRow["yewuyuanID"] = drWeb["cod_representante"];
		newRow["py"] = drWeb["representante"];
		newRow["total"] = drWeb["total"];
 
		dt.Rows.Add(newRow);
	}
 
	MySqlDataAdapter daPen = new MySqlDataAdapter(querypendiente, CadenaConexionGestion);
 
	DataTable dPen = new DataTable();
	daPen.Fill(dPen);
 
	for (int i = 0; i < dPen.Rows.Count; i++)  //Tabla Pendientes
	{
		DataRow drPen = dPen.Rows[i];
 
		DataRow newRow = dt.NewRow();
 
		newRow["id"] = drPen["pedidocodigo"];
		newRow["pedidokey"] = drPen["pedidokey2"];
		newRow["riqi"] = drPen["fecha_creacion"];
		newRow["fecha_c"] = drPen["fecha_creacion"];
		newRow["kehuId"] = drPen["cod_cliente"];
		newRow["name"] = drPen["namecliente"];
		newRow["yewuyuanID"] = drPen["cod_representante"];
		newRow["py"] = drPen["representante"];
		newRow["total"] = drPen["total"];
 
		dt.Rows.Add(newRow);
	}
 
	MySqlDataAdapter daDipo = new MySqlDataAdapter(querydipo, CadenaConexionGestion);
 
	DataTable dDipo = new DataTable();
	daDipo.Fill(dDipo);
 
	for (int i = 0; i < dDipo.Rows.Count; i++)  //Tabla DIPO
	{
		DataRow drDipo = dDipo.Rows[i];
 
		DataRow newRow = dt.NewRow();
 
		newRow["id"] = drDipo["pedidocodigo"];
		newRow["pedidokey"] = drDipo["pedidokey"];
		newRow["riqi"] = drDipo["fecha_creacion"];
		newRow["fecha_c"] = drDipo["fecha_creacion"];
		newRow["kehuId"] = drDipo["cod_cliente"];
		newRow["name"] = drDipo["namecliente"];
		newRow["yewuyuanID"] = drDipo["cod_representante"];
		newRow["py"] = drDipo["representante"];
		newRow["total"] = drDipo["total"];
 
		dt.Rows.Add(newRow);
	}
 
	//Ordenamos el datatable
	DataView dv = dt.DefaultView;
	dv.Sort = "fecha_c desc";
	DataTable sortedDT = dv.ToTable();
	this.dataGridView1.DataSource = sortedDT;
 
	//Filtra al buscar
	this.mifiltro = sortedDT.DefaultView;
	this.dataGridView1.DataSource = mifiltro;
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Ordenar un data table

Publicado por David (60 intervenciones) el 29/10/2015 00:04:33
Por lo que veo lo rellenas desde una base de datos, por lo tanto donde tienes que mirar el tipo del campo es en la tabla 'pedidolis' de donde estás recogiendo los datos, a ver si es varchar o datetime.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Ordenar un data table

Publicado por Javier Lopez Vargas (21 intervenciones) el 27/05/2016 07:46:32
Si los datos ya están en el objeto DataTable, no se pueden ordenar. Ahora bien, lo que sí puedes hacer es crearte un objeto DataView con los datos del objeto DataTable, el cual podrás ordenarlo por el campo que desees:

1
2
3
4
5
6
7
8
9
Dim view As DataView = New DataView(objetoDataTable)
 
     ' Lo ordenamos por el campo Nombre.
     '
     view.Sort = "Nombre"
 
     ' Mostramos los datos en un control DataGridView
     '
     DataGridView1.DataSource = view

Y hablando del control DataGridView, en éste control puedes ordenar los datos haciendo clic en el encabezado de sus columnas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar