
31/12/2007, 07:07
|
 | Colaborador | | Fecha de Ingreso: septiembre-2007 Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 17 años, 6 meses Puntos: 87 | |
Re: Mostrar datos desde un ComboBox public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=.;initial catalog=deepra;uid=sa;pwd=;");
SqlDataAdapter ad;
CurrencyManager cur;
SqlCommandBuilder cbuilder;
DataSet ds = new DataSet();
DataRow dr;
string PageAction = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ad = new SqlDataAdapter("select * from Student", con);
cbuilder = new SqlCommandBuilder(ad);
ad.InsertCommand = cbuilder.GetInsertCommand();
ad.UpdateCommand = cbuilder.GetUpdateCommand();
ad.DeleteCommand = cbuilder.GetDeleteCommand();
ad.Fill(ds, "Stud"); TxtRoll.DataBindings.Add("Text", ds, "Stud.Roll");
TxtName.DataBindings.Add("Text", ds, "Stud.Name");
TxtAddress.DataBindings.Add("Text", ds, "Stud.Address");
cur = (CurrencyManager)this.BindingContext[ds, "Stud"];
}
private void BtnFirst_Click(object sender, EventArgs e)
{
cur.Position = 0;
}
private void BtnPrevious_Click(object sender, EventArgs e)
{
cur.Position -= 1;
}
private void BtnNext_Click(object sender, EventArgs e)
{
cur.Position += 1;
}
private void BtnLast_Click(object sender, EventArgs e)
{
cur.Position = cur.Count - 1;
}
private void BtnNew_Click(object sender, EventArgs e)
{
dr = ds.Tables["Stud"].NewRow();
TxtRoll.Text = "";
TxtName.Text = "";
TxtAddress.Text = "";
PageAction = "Add";
}
private void BtnSave_Click(object sender, EventArgs e)
{
if (PageAction == "Add")
{
dr[0] = Int32.Parse(TxtRoll.Text);
dr[1] = TxtName.Text;
dr[2] = TxtAddress.Text;
ds.Tables["Stud"].Rows.Add(dr);
ad.Update(ds.Tables["Stud"]);
}
else
{
dr = ds.Tables["Stud"].Rows[cur.Position];
dr.BeginEdit();
dr[0] = Int32.Parse(TxtRoll.Text);
dr[1] = TxtName.Text;
dr[2] = TxtAddress.Text;
dr.EndEdit();
ad.Update(ds.Tables["Stud"]);
}
}
private void BtnDelete_Click(object sender, EventArgs e)
{
ds.Tables["Stud"].Rows[cur.Position].Delete();
ad.Update(ds.Tables["Stud"]);
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
cur.Position=comboBox1.SelectedIndex;
}
Saludos
Peterpay |