
Cargar Datagridview
Publicado por Ruben Dario (23 intervenciones) el 17/12/2015 17:25:18
Buenas tardes
estoy intentando cargar un datagridvie de distintas BD y Distintas tablas lo quiero hacer a traves de un data table
entonces tengo esto pero no carga mi datatable dt.
primero
luego tengo esto
y por ultimo el load
Me podrian ayudar?
Muchas gracias
estoy intentando cargar un datagridvie de distintas BD y Distintas tablas lo quiero hacer a traves de un data table
entonces tengo esto pero no carga mi datatable dt.
primero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("pedidokey");
dt.Columns.Add("riqi", typeof(DateTime));
dt.Columns.Add("fecha_c", typeof(DateTime));
dt.Columns.Add("kehuID");
dt.Columns.Add("name");
dt.Columns.Add("yewuyuanID");
dt.Columns.Add("py");
dt.Columns.Add("total", typeof(double));
dt.Columns.Add("color");
return dt;
1
2
3
4
5
6
7
8
9
10
11
private void AddValuesToTable(DataTable dt, DataRowCollection rows)
{
foreach (DataRow row in rows)
{
DataRow newRow = dt.NewRow();
foreach (DataColumn column in row.Table.Columns)
{
newRow[column.ColumnName] = row[column.ColumnName];
}
}
}
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
145
146
147
private void Form1_Load(object sender, EventArgs e)
{
DateTime dte = DateTime.Now; //Cragamos la fecha en una variable
dte = dte.AddMonths(-3); //Restamos un mes a la fecha desde
dateTimePicker1.Value = dte; //Asignamos la variable al datetimerpicker
DateTime dte1 = dte.Date;
// Cargamos Pedidos
DataTable dt = GetDataTable();
try
{
DataTable dPedidos = new DataTable();
using (MySqlConnection cnges = new MySqlConnection(CadenaConexion))
{
string querypedido = @"Select p.id, p.pedidokey, p.riqi, p.fecha_c, p.kehuID, c.name, p.yewuyuanID, v.py, p.jinez as total
from pedidolist p, kehu c, yewuyuan v
where p.kehuid = c.bianhao and p.yewuyuanID = v.bianhao and p.riqi >= @fecha Order by p.id ASC";
MySqlCommand cmdD = new MySqlCommand(querypedido, cnges);
cmdD.Parameters.AddWithValue("@fecha", dte.Date);
MySqlDataAdapter da = new MySqlDataAdapter(cmdD);
da.Fill(dPedidos);
cnges.Close();
}
AddValuesToTable(dt, dPedidos.Rows);
// Cargamos Web
DataTable dWeb = new DataTable();
using (MySqlConnection cnges = new MySqlConnection(CadenaConexionGestion))
{
string queryweb = @"select c.pedidocodigo as id, c.cod_cliente as kehuID, c.namecliente as name, c.cod_representante as yewuyuanID
, c.representante as py, c.fecha_creacion as riqi, c.fecha_creacion as fecha_c, c.pedidokey, c.total
from pedidocabeceraweb c
where c.fecha_creacion >= @fecha Order by c.pedidocodigo ASC"; //Query Pedido Web
MySqlCommand cmdD = new MySqlCommand(queryweb, cnges);
cmdD.Parameters.AddWithValue("@fecha", dte.Date);
MySqlDataAdapter daWeb = new MySqlDataAdapter(cmdD);
daWeb.Fill(dWeb);
cnges.Close();
}
AddValuesToTable(dt, dWeb.Rows);
// Cargamos Pendientes
DataTable dPen = new DataTable();
using (MySqlConnection cnges = new MySqlConnection(CadenaConexionGestion))
{
string querypendiente = @"select c.pedidocodigo as id, c.cod_cliente as kehuId, c.namecliente as name, c.cod_representante as yewuyuanID
, c.representante as py, c.fecha_creacion as riqi, c.fecha_creacion as fecha_c, c.pedidokey2 as pedidokey, c.total
from pedidocabecera c
where c.enviado = 1 and c.fecha_creacion >= @fecha Order by c.pedidocodigo ASC"; //Query Pedido Pendiente
MySqlCommand cmdD = new MySqlCommand(querypendiente, cnges);
cmdD.Parameters.AddWithValue("@fecha", dte.Date);
MySqlDataAdapter da = new MySqlDataAdapter(cmdD);
da.Fill(dPen);
cnges.Close();
}
AddValuesToTable(dt, dPen.Rows);
// Cargamos Dipo
DataTable dDipo = new DataTable();
using (MySqlConnection cnges = new MySqlConnection(CadenaConexionGestion))
{
string querydipo = @"select c.pedidocodigo as id, c.cod_cliente as kehuId, c.namecliente as name, c.cod_representante as yewuyuanID
, c.representante as py, c.fecha_creacion as riqi, c.fecha_creacion as fecha_c, c.pedidokey, c.total
from pedidocabeceradipo c
where c.fecha_creacion >= @fecha Order by c.pedidocodigo ASC"; //Query Pedido Dipo
MySqlCommand cmdD = new MySqlCommand(querydipo, cnges);
cmdD.Parameters.AddWithValue("@fecha", dte.Date);
MySqlDataAdapter da = new MySqlDataAdapter(cmdD);
da.Fill(dDipo);
cnges.Close();
}
AddValuesToTable(dt, dDipo.Rows);
//Cargamos Poee
DataTable dPoee = new DataTable();
using (MySqlConnection cnges = new MySqlConnection(CadenaConexionGestion))
{
string querypoee = @"select pedidokey, color
from poee
where fechapedido >= @fecha Order by pedidokey ASC";
MySqlCommand cmdD = new MySqlCommand(querypoee, cnges);
cmdD.Parameters.AddWithValue("@fecha", dte.Date);
MySqlDataAdapter daPoee = new MySqlDataAdapter(cmdD);
daPoee.Fill(dPoee);
cnges.Close();
}
var colorRows = dPoee.Rows.Cast<DataRow>();
foreach (DataRow item in dt.Rows)
{
DataRow colorRow = colorRows
.FirstOrDefault(c => ((string)c["pedidokey"]).Trim() == ((string)item["pedidokey"]).Trim());
if (colorRow != null) item["color"] = colorRow["color"];
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString()); //Capturamos el error si existe
}
//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;
//Establecemos el Ancho
dataGridView1.Columns["id"].Width = 42;
dataGridView1.Columns["pedidokey"].Width = 95;
dataGridView1.Columns["riqi"].Width = 100;
dataGridView1.Columns["fecha_c"].Width = 100;
dataGridView1.Columns["kehuID"].Width = 68;
dataGridView1.Columns["name"].Width = 127;
dataGridView1.Columns["yewuyuanID"].Width = 65;
dataGridView1.Columns["py"].Width = 129;
dataGridView1.Columns["total"].Width = 68;
// Establecemos el Nombre de columnas
dataGridView1.Columns[0].HeaderText = "ID";
dataGridView1.Columns[1].HeaderText = "Pedido";
dataGridView1.Columns[2].HeaderText = "Fecha Pedido";
dataGridView1.Columns[3].HeaderText = "Envio Pedido";
dataGridView1.Columns[4].HeaderText = "Codigo Cliente";
dataGridView1.Columns[5].HeaderText = "Cliente";
dataGridView1.Columns[6].HeaderText = "Codigo Vendedor";
dataGridView1.Columns[7].HeaderText = "Vendedor";
dataGridView1.Columns[8].HeaderText = "Total";
this.dataGridView2.RowsDefaultCellStyle.BackColor = Color.Bisque;
this.dataGridView2.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
}
Me podrian ayudar?
Muchas gracias
Valora esta pregunta


0