Retroceder   Foros del Web > Programación para sitios web > .NET

Respuesta
 
Herramientas Desplegado
Antiguo 06-jul-2008, 10:20   #1 (permalink)
robertgustavo está en el buen camino
 
Avatar de robertgustavo
 
Fecha de Ingreso: marzo-2008
Mensajes: 129
objetos a nivel formulario en c#

hola, como se declara objetos a nivel en C#
tengo un void que se llama cargardatos asi:
___________________________________

private void CargarDatos()
{
oledbconnection Cn=new oledbconnection();
oledbdataadapter Da=new oledbdataadapter();
dataset ds=new dataset();
try
{
da.fill(ds,"clientes");
datagridview.datasource=ds.tables["clientes"]
}
}

private void Nuevo()
{
//desde aca no puedo usar "Cn", la coneccion o el "Ds", dataset

}

como hago para usarlos en cualquier parte del formulario
robertgustavo está desconectado   Responder Citando
Antiguo 06-jul-2008, 10:53   #2 (permalink)
Peterpay tiene algunos puntos positivos de karma
 
Avatar de Peterpay
 
Fecha de Ingreso: septiembre-2007
Ubicación: Mexico
Mensajes: 2.044
Enviar un mensaje por MSN a Peterpay Enviar un mensaje por Skype™ a Peterpay
Respuesta: objetos a nivel formulario en c#

Cn y Ds y Da tendrian q estar nivel del inicio de tu class formulario

inmediatamnete despues. de

public Class Fomulario1:Form
{
---> tus propiedades o miembros
--->tus metoods
}
__________________
Saludos
Peterpay
MCPD EAD
Directorio Gratuito
Peterpay está desconectado   Responder Citando
Antiguo 06-jul-2008, 17:26   #3 (permalink)
robertgustavo está en el buen camino
 
Avatar de robertgustavo
 
Fecha de Ingreso: marzo-2008
Mensajes: 129
Respuesta: objetos a nivel formulario en c#

no funciona
en esta parte:

OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);

Cn sale marcado dice:


Error 1 Un inicializador de campo no puede hacer referencia al campo no estático, método o propiedad 'Myprogram.FrmArticulos.Cn' C:\Documents and Settings\Roberth\Mis documentos\Visual Studio 2005\Projects\myprogram\myprogram\FrmArticulos.cs 17 79Myprogram

__________________________________________________ ______________
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace MyProgram
{
public partial class FrmArticulos : Form

{
OleDbConnection Cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\SismasDB.mdb");
OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);
DataSet Ds = new DataSet();
DataView Dv = new DataView();
BindingSource Bs = new BindingSource();
public int Pos;
public FrmArticulos()
{
InitializeComponent();
}
private void FrmArticulos_Load(object sender, EventArgs e)
{
LlenarDatos();
FormatearGrid();
}
void LlenarDatos()
{
try
{
Da.Fill(Ds, "Articulos");
this.DgArticulos.DataSource = Ds.Tables["Articulos"];
Bs.DataSource = Ds.Tables["Articulos"];
}
catch (OleDbException ex)
{
MessageBox.Show("No se pudo conectar al origen de datos,compruebe que exista o reinstale la aplicación", "Error Inesperado", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
finally
{
Ds.Clear();
Da.Fill(Ds, "Articulos");
}
}
private void FormatearGrid()
{
this.DgArticulos.Columns[0].Width = 60;
this.DgArticulos.Columns[1].Width = 120;
}
private void CerrarArticulosForm(object sender, EventArgs e)
{
this.Close();
}

private void CmdBuscarImagen_Click(object sender, EventArgs e)
{
if (OFDBuscaImagen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.ImagePreview.Image = Image.FromFile(this.OFDBuscaImagen.FileName);
}
}
private void habilitarControles()
{
this.TxtCodigo.Enabled = true;
this.TxtPrecio.Enabled = true;
this.TxtPrecioCompra.Enabled = true;
this.TxtPrecioVenta.Enabled = true;
this.TxtProducto.Enabled = true;
this.TxtStock.Enabled = true;
this.TxtStockMaximo.Enabled = true;
this.TxtStockMaximo.Enabled = true;
this.CboCategoria.Enabled = true;
this.CboMarca.Enabled = true;
this.CboMedida.Enabled = true;
this.CboProveedor.Enabled = true;
this.CmdSelect1.Enabled = true;
this.CmdSelect2.Enabled = true;
this.CmdSelect3.Enabled = true;
this.CmdSelect4.Enabled = true;
this.CmdBuscarImagen.Enabled = true;
}
private void DeshabilitarControles()
{
this.TxtCodigo.Enabled = false;
this.TxtPrecio.Enabled = false;
this.TxtPrecioCompra.Enabled = false;
this.TxtPrecioVenta.Enabled = false;
this.TxtProducto.Enabled = false;
this.TxtStock.Enabled = false;
this.TxtStockMaximo.Enabled = false;
this.TxtStockMaximo.Enabled = false;
this.CboCategoria.Enabled = false;
this.CboMarca.Enabled = false;
this.CboMedida.Enabled = false;
this.CboProveedor.Enabled = false;
this.CmdSelect1.Enabled = false;
this.CmdSelect2.Enabled = false;
this.CmdSelect3.Enabled = false;
this.CmdSelect4.Enabled = false;
this.CmdBuscarImagen.Enabled = false;

}

private void CmdNuevo_Click(object sender, EventArgs e)
{
habilitarControles();
CmdGuardar.Enabled = true;
CmdNuevo.Enabled = false;
}
private void Nuevo()
{

}

private void CmdGuardar_Click(object sender, EventArgs e)
{
DeshabilitarControles();
CmdNuevo.Enabled=true;
CmdGuardar.Enabled=false;
}

}
}
robertgustavo está desconectado   Responder Citando
Antiguo 07-jul-2008, 07:28   #4 (permalink)
Peterpay tiene algunos puntos positivos de karma
 
Avatar de Peterpay
 
Fecha de Ingreso: septiembre-2007
Ubicación: Mexico
Mensajes: 2.044
Enviar un mensaje por MSN a Peterpay Enviar un mensaje por Skype™ a Peterpay
Respuesta: objetos a nivel formulario en c#

Si pero puedes poner todo la inicializacion del connection dentro del form load.

si fuece asi tu variable tendria q ser estatica q no es el caso solo quieres q sea globlal.

public partial class FrmArticulos : Form

{
OleDbConnection Cn;
OleDbDataAdapter Da;
DataSet Ds;
DataView Dv;
BindingSource Bs;
public int Pos;
public FrmArticulos()
{
OleDbConnection Cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\SismasDB.mdb");
OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);
DataSet Ds = new DataSet();
DataView Dv = new DataView();
BindingSource Bs = new BindingSource();
public int Pos;

InitializeComponent();
}

con eso tus variables son de ambito global dentro de tu forma y las inicializas cuando se crea tu forma.
__________________
Saludos
Peterpay
MCPD EAD
Directorio Gratuito
Peterpay está desconectado   Responder Citando
Antiguo 07-jul-2008, 08:27   #5 (permalink)
robertgustavo está en el buen camino
 
Avatar de robertgustavo
 
Fecha de Ingreso: marzo-2008
Mensajes: 129
Respuesta: objetos a nivel formulario en c#

yes, salio!! gracias Peterpay
Saludos
robertgustavo está desconectado   Responder Citando
Respuesta

No hay votos aún.


Herramientas
Desplegado

Normas de Publicación
No puedes crear nuevos temas
No puedes responder temas
No puedes subir archivos adjuntos
No puedes editar tus mensajes

BB code is Activado
Caritas están Activado
[IMG] está Activado
Código HTML está Desactivado


La Zona horaria es GMT -6. Ahora son las 03:12.


Message Board Statistics

LinkBacks Enabled by vBSEO 3.1.0

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