C sharp - C#. Cambiar código de recepción de datos de un ListBox a un DataGrid

 
Vista:

C#. Cambiar código de recepción de datos de un ListBox a un DataGrid

Publicado por Tonin (1 intervención) el 29/05/2017 18:20:19
Hola! Tenía una consulta. Estoy realizando la programación de una PDA en Visual Studio 2008 por cuestiones de compatibilidad. Total, que en uno de los formularios hay un textbox donde se escanea la etiqueta y la pasa a un Listbox. Hasta aquí sin problemas. Pero ahora quieren unas modificaciones y tengo que cambiar el listbox de registro de las etiquetas por un Datagrid. He trabajado con Datagridview pero por motivos que desconozco, solo me deja usar el Datagrid. He intentado varias formas pero no me funciona. Como tendré que cambiar el código para que las etiquetas introducidas en el textbox se me vayan registrando en el Datagrid?

Pongo código para orientar. Dónde pone lista ahora tiene que ser datagrid, con todas las funciones (ordenar, registrar, etc... )

FIJARSE POR FAVOR EN MÉTODOS: guardar, ordenarlista y existeetiqueta.

Muchas gracias, un saludo. Espero respuesta pronto.

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
 
 
namespace escan
{
 
 
    public partial class escanear : Form
    {
        public string elmodo;
        public string eldestino;
        public string accion;
        public int lasT;
        public string tipoFiltro = "D";
        //public int contadorRegistros;
 
 
        public escanear()
        {
            InitializeComponent();
            lista.Items.Clear();
            Inicia();
            lasT = 0;
            contador.Text = "0";
            eti1.Focus();
        }
 
        private void Descarga()
        {
            string enviar = "";
            string resultado = "";
            bool ok = false;
            sending.Visible = true;
            try
            {
                if (File.Exists(@"Vdiario.xml"))
                {
                    // Realizar descarga, pasa a historico y borrar
                    enviar = Clases.Clases.ESCANER.LeerCompleto();
                    if (enviar != "Err")
                    {
 
                        WebReference.ServicioViaXpressdeTrazabilidad Ws = new escan.WebReference.ServicioViaXpressdeTrazabilidad();
                        resultado = Ws.Descarga(enviar, "12wxJLD2437%");
                        if (resultado == "Ok")
                        {
                            Clases.Config.ultimadescarga = DateTime.Now.ToString("dd/MM/yy hh:mm");
                            Clases.Config.ModiConfig();
                            Clases.Clases.BorraEnviados();
                            ok = true;
                        }
 
 
                }
 
 
            }
            catch (Exception ex)
            {
                ok = false;
                sending.Visible = false;
                MessageBox.Show("Error de transmisión");
            }
            if (ok) { MessageBox.Show("Transmisión realizada !!!"); }
            sending.Visible = false;
        }
 
 
        private bool ExisteEtiqueta(string etiqueta)  // Función que comprueba la repetición de etiquetas en la Lista
        {
            bool resultado = false;
            for (int x = 0; x < lista.Items.Count; x++)
            {
                if (lista.Items[x].ToString().IndexOf(etiqueta) > -1)
                    resultado = true;
            }
            return resultado;
        }
 
        private void Entrada()
        {
            string etiqueta = "";
 
 
            if (elmodo == "ESCAN" || elmodo == "ONBOARD")
            {
 
                if (eti1.Text.Length == 13)
                {
 
                    if (eti1.Text.Substring(12, 1) == "D")
                    {
 
                        if (!ExisteEtiqueta(eti1.Text))
                        {
                            modo_D();
                        }
                        else
                        {
                            eti1.Text = "";
                            eti1.Focus();
                            System.Media.SystemSounds.Question.Play();
                        }
 
                    }
                    else
                        if ((eti1.Text.Substring(12, 1) == "P"))
                        {
 
                            if (!ExisteEtiqueta(eti1.Text))
                            {
                                Guardar(eti1.Text);
                            }
                            else
                            {
                                eti1.Text = "";
                                eti1.Focus();
                                System.Media.SystemSounds.Question.Play();
                            }
 
 
                        }
                }
                if (eti1.Text.Length == 12)
                {
                    // Modo T
                    string search = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Patrón
                    string search2 = "12";
                    int est = search.IndexOf(eti1.Text.Substring(0, 1).ToUpper()); //Empieza por letra?? Index devuelve -1 si no lo encuentra por eso el >= 0
                    int est2 = search.IndexOf(eti1.Text.Substring(1, 2).ToUpper());
                    int est3 = search2.IndexOf(eti1.Text.Substring(0, 1));
 
                    if ((est >= 0) || (est2 >= 0) || (est3 >= 0))
                    {
                        etiqueta = eti1.Text + "T";
 
                        if (!ExisteEtiqueta(etiqueta))
                        {
                            Guardar(etiqueta);
                            lasT++; // Ellos quieren contador de T
                            label2.Text = lasT.ToString();
                        }
                        else
                        {
                            eti1.Text = "";
                            eti1.Focus();
                            System.Media.SystemSounds.Question.Play();
                        }
 
                    }
 
                    eti1.Text = "";
                    eti1.Focus();
                }
            }
            else // Modo FILTRO
            {
                if (validarEntrada())
                {
                    if (!ExisteEtiqueta(eti1.Text))
                    {
                        Guardar(eti1.Text);
                        eti1.Text = "";
                        eti1.Focus();
                    }
                    else
                    {
                        eti1.Text = "";
                        eti1.Focus();
                        System.Media.SystemSounds.Question.Play();
                    }
                }
                else
                {
                    eti1.Text = "";
                    System.Media.SystemSounds.Question.Play();
                    eti1.Focus();
                }
            }
 
        }
 
        private bool validarEntrada()
        {
            bool resultado = true;
            if (eti1.Text.Length == 13)
            {
                if (tipoFiltro != eti1.Text.Substring(12, 1))
                {
                    resultado = false;
                }
 
 
            }
            if (eti1.Text.Length == 12)
            {
                if (tipoFiltro != "T")
                {
                    resultado = false;
                }
                else
                {
                    string search = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Patrón
                    string search2 = "12";
                    int est = search.IndexOf(eti1.Text.Substring(0, 1).ToUpper()); //Empieza por letra?? Index devuelve -1 si no lo encuentra por eso el >= 0
                    int est2 = search.IndexOf(eti1.Text.Substring(1, 2).ToUpper());
                    int est3 = search2.IndexOf(eti1.Text.Substring(0, 1));
                    if ((est >= 0) || (est2 >= 0) || (est3 >= 0))
                    {
                        eti1.Text = eti1.Text + "T";
                        lasT++;
                        label2.Text = lasT.ToString();
                    }
                    //else
                    //{
                    //    eti1.Text = "";
                    //    eti1.Focus();
                    //    System.Media.SystemSounds.Exclamation.Play();
                    //}
                    //string search2 = "1234567890";
                    //int est2 = search2.IndexOf(eti1.Text.Substring(0, 1));
                    //if (est2 >= 0)
                    //{
                    //    eti1.Text = "";
                    //    eti1.Focus();
                    //    System.Media.SystemSounds.Exclamation.Play();
                    //}
 
                }
            }
            return resultado;
        }
 
        private void eti1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Si se pulsa la tecla Intro, pasar al siguiente
            //if( e.KeyChar == Convert.ToChar('\r') ){
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                Entrada();
            }
            if (eti1.Text.Length == 13)
            {
                Entrada();
            }
        }
        private void GuardarD()
        {
            Guardar(eti1.Text);
        }
 
        private void ordena_lista()
        {
            int cuantos = lista.Items.Count - 1;
            if (cuantos > 0)
            {
                string[] arr = new string[cuantos + 1];
                for (int i = 0; i <= cuantos; i++)
                {
                    arr[i] = lista.Items[i].ToString();
                }
                lista.Items.Clear();
                for (int x = cuantos; x >= 0; x--)
                {
                    lista.Items.Add(arr[x]);
                }
            }
        }
        private void Guardar(string etiqueta)
        {
 
            label2.Visible = true;
            if (Form1.descargando == false)
            {
                if (accion == "") { accion = "   "; }
 
                if (!Clases.Clases.ESCANER.Inserta(Clases.Config.baseescaner, Clases.Config.usuario, etiqueta, accion, eldestino, ""))
                {
                    MessageBox.Show("No se ha podido guardar la etiqueta");
                }
                else
                {
                    lista.Items.Add(etiqueta + " " + accion + " " + eldestino);
                    contador.Text = lista.Items.Count.ToString();  //Contador del número de etiquetas añadidas a lista
                    ordena_lista();
 
                    //contadorRegistros++;
                    //contador.text = contadorRegistros.ToString();
 
                    //waveOutSetVolume(IntPtr.Zero, -858993460);
                    System.Media.SystemSounds.Exclamation.Play();
                    //System.Media.SystemSounds.Beep.Play();
                    eti1.Text = "";
                    eti1.Focus();
                }
            }
        }
 
 
        private void escanear_Closed(object sender, EventArgs e)
        {
            Descarga();
        }
 
    }
 
    }
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