Ver Mensaje Individual
  #3 (permalink)  
Antiguo 14/02/2011, 09:45
Oscar_Santander
 
Fecha de Ingreso: enero-2011
Ubicación: La Serena
Mensajes: 28
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: como pasar todo el contenido datagreedview a una variable

Código vb:
Ver original
  1. Dim conectar As New OleDbConnection(conexion)
  2.         conectar.Open()
  3.         'borra las columnas del datagreedview
  4.        Me.DataGridView1.DataSource = ""
  5.         Me.DataGridView1.Columns.Clear()
  6.         If Me.ComboBox1.Text <> "Seleccionar Establecimiento" And Me.ComboBox2.Text <> "" Then
  7.             Dim mosene As New OleDb.OleDbDataAdapter("select DISTINCT (d.CodigoPrestacion) as Código, p.NOMBRE_PRESTACION as Prestación, p.REM_PRESTACION as Rem, p.SECCION as Sección, d.Col01 as Enero from DatosSerieA d, PRESTACIONES p where d.CodigoPrestacion=p.CODIGO_PRESTACION and d.IdEstablecimiento='" & Me.Label7.Text & "' and d.Mes=1 and Ano=" & Me.ComboBox2.Text & "", conectar)
  8.             Dim datos As New DataSet
  9.             mosene.Fill(datos)
  10.             Me.DataGridView1.DataSource = datos.Tables(0)
  11.            
  12.             Dim pb As ToolStripProgressBar = MDIParent1.ToolStripProgressBar1
  13.             pb.Visible = True
  14.             pb.Maximum = Me.DataGridView1.RowCount - 1
  15.             pb.Step = 1
  16.  
  17.  
  18.             '--------------------------------------------------------------------
  19.            'crea las columnas del datagredview
  20.            '--------------------------------------------------------------------
  21.            Me.DataGridView1.Columns.Add("Febrero", "Febrero")
  22.             Me.DataGridView1.Columns.Add("Marzo", "Marzo")
  23.             Me.DataGridView1.Columns.Add("Abril", "Abril")
  24.             Me.DataGridView1.Columns.Add("Mayo", "Mayo")
  25.             Me.DataGridView1.Columns.Add("Junio", "Junio")
  26.             Me.DataGridView1.Columns.Add("Julio", "Julio")
  27.             Me.DataGridView1.Columns.Add("Agosto", "Agosto")
  28.             Me.DataGridView1.Columns.Add("Septiembre", "Septiembre")
  29.             Me.DataGridView1.Columns.Add("Octubre", "Octubre")
  30.             Me.DataGridView1.Columns.Add("Noviembre", "Noviembre")
  31.             Me.DataGridView1.Columns.Add("Diciembre", "Diciembre")
  32.             Me.DataGridView1.Columns.Add("Total", "Total")
  33.             '--------------------------------------------------------------------
  34.            '--------------------------------------------------------------------
  35.  
  36.             '--------------------------------------------------------------------
  37.            'inserta el valor corespondiente a cada mes
  38.            '--------------------------------------------------------------------
  39.            For i As Integer = 0 To Me.DataGridView1.RowCount - 2
  40.                 Dim feb As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 2 and Ano=" & Me.ComboBox2.Text & "", conectar)
  41.                 Dim mar As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 3 and Ano=" & Me.ComboBox2.Text & "", conectar)
  42.                 Dim abr As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 4 and Ano=" & Me.ComboBox2.Text & "", conectar)
  43.                 Dim may As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 5 and Ano=" & Me.ComboBox2.Text & "", conectar)
  44.                 Dim jun As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 6 and Ano=" & Me.ComboBox2.Text & "", conectar)
  45.                 Dim jul As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 7 and Ano=" & Me.ComboBox2.Text & "", conectar)
  46.                 Dim ago As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 8 and Ano=" & Me.ComboBox2.Text & "", conectar)
  47.                 Dim sep As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 9 and Ano=" & Me.ComboBox2.Text & "", conectar)
  48.                 Dim oct As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 10 and Ano=" & Me.ComboBox2.Text & "", conectar)
  49.                 Dim nov As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 11 and Ano=" & Me.ComboBox2.Text & "", conectar)
  50.                 Dim dic As New OleDb.OleDbCommand("select Col01 from DatosSerieA where CodigoPrestacion='" & Me.DataGridView1.Rows(i).Cells("Código").Value & "' and IdEstablecimiento='" & Me.Label7.Text & "' and Mes= 12 and Ano=" & Me.ComboBox2.Text & "", conectar)
  51.  
  52.                 Try
  53.                     Me.DataGridView1.Item(5, i).Value = feb.ExecuteScalar()
  54.                     Me.DataGridView1.Item(6, i).Value = mar.ExecuteScalar()
  55.                     Me.DataGridView1.Item(7, i).Value = abr.ExecuteScalar()
  56.                     Me.DataGridView1.Item(8, i).Value = may.ExecuteScalar()
  57.                     Me.DataGridView1.Item(9, i).Value = jun.ExecuteScalar()
  58.                     Me.DataGridView1.Item(10, i).Value = jul.ExecuteScalar()
  59.                     Me.DataGridView1.Item(11, i).Value = ago.ExecuteScalar()
  60.                     Me.DataGridView1.Item(12, i).Value = sep.ExecuteScalar()
  61.                     Me.DataGridView1.Item(13, i).Value = oct.ExecuteScalar()
  62.                     Me.DataGridView1.Item(14, i).Value = nov.ExecuteScalar()
  63.                     Me.DataGridView1.Item(15, i).Value = dic.ExecuteScalar()
  64.                 Catch ex As Exception
  65.                     MsgBox(ex.ToString)
  66.                 End Try
  67.                 pb.Value += 1
  68.             Next
  69.             '--------------------------------------------------------------------
  70.            '--------------------------------------------------------------------
  71.  
  72.             '--------------------------------------------------------------------
  73.            'suma las columnas de enero a diciembre
  74.            '--------------------------------------------------------------------
  75.            Dim suma As Integer = 0
  76.             For i As Integer = 0 To DataGridView1.Rows.Count - 2
  77.                 For x As Integer = 4 To DataGridView1.Columns.Count - 2
  78.                     If IsDBNull(DataGridView1.Rows(i).Cells(x).Value) Then
  79.  
  80.                     Else
  81.                         suma = suma + DataGridView1.Rows(i).Cells(x).Value
  82.                     End If
  83.                     Me.DataGridView1.Item(16, i).Value = suma
  84.                 Next
  85.                 suma = 0
  86.             Next
  87.  
  88.             pb.Value = 0
  89.             pb.Visible = False
  90.             '--------------------------------------------------------------------
  91.            '--------------------------------------------------------------------