protected void grd_suppliers_RowCommand(object sender, GridViewCommandEventArgs e) { /* * No se puede porque no se ha seleccioando ninguna linea * GridViewRow fila = grd_suppliers.SelectedRow; * int id = Convert.ToInt32(grd_suppliers.DataKeys[fila.RowIndex].Value);*/ // ELIMINA UN REGISTRO if (e.CommandName == "eliminar") { // obtenemos el ID NorthwindDSDataContext ds = new NorthwindDSDataContext(); int id = Convert.ToInt32(e.CommandArgument); var supplierTemp = (from item in ds.Suppliers where item.SupplierID == id select item).Single(); ds.Suppliers.DeleteOnSubmit(supplierTemp); ds.SubmitChanges(); grd_suppliers.DataBind(); } // LLENA LOS DATOS DEL FORMULARIO DE MODIFICACION else if (e.CommandName == "modificar") { // obtenemos el ID NorthwindDSDataContext ds = new NorthwindDSDataContext(); int id = Convert.ToInt32(e.CommandArgument); btn_insertar_modificar.Text = "Modificar"; //obtenemos los datos mediante el id var supplierTemp = (from item in ds.Suppliers where item.SupplierID == id select new { item.CompanyName, item.ContactName, item.Address, item.Phone }).Single(); // llenamos los datos en el formulario lbl_id.Text = id.ToString(); txt_comanyName.Text = supplierTemp.CompanyName; txt_contactName.Text = supplierTemp.ContactName; txt_direccion.Text = supplierTemp.Address; txt_telefono.Text = supplierTemp.Phone; } }
protected void btn_insertar_modificar_Click(object sender, EventArgs e) { NorthwindDSDataContext ds = new NorthwindDSDataContext(); if (btn_insertar_modificar.Text == "Agregar") { //creamos el objeto Supplier temp = new Supplier(); temp.CompanyName = txt_comanyName.Text; temp.ContactName = txt_contactName.Text; temp.Address = txt_direccion.Text; temp.Phone = txt_telefono.Text; //lo agregamos ds.Suppliers.InsertOnSubmit(temp); ds.SubmitChanges(); grd_suppliers.DataBind(); } else if (btn_insertar_modificar.Text == "Modificar") { // obtenemos el id int id = Convert.ToInt32(lbl_id.Text); //obtenemos la fuente de datos var suppliersTemp = (from item in ds.Suppliers where item.SupplierID == id select item).Single(); // hacemos las modificaciones suppliersTemp.CompanyName = txt_comanyName.Text; suppliersTemp.ContactName = txt_contactName.Text; suppliersTemp.Address = txt_direccion.Text; suppliersTemp.Phone = txt_telefono.Text; // actualizamos ds.SubmitChanges(); grd_suppliers.DataBind(); } }