Jump to content

Recommended Posts

Posted

Bons dias.

Tenho uma gridview, em que é tudo feito pelo código C#.

Nela quero que seja possível inserir como alterar dados.

Para inserir uso um botão "Adicionar" externo à tabela, mas associado a ela e, para alterar uso uma hyperlink existente na tabela.

Deste genero: Rwxmy.jpg

Só que agora tou com um problema. O Botão "Adicionar" deixou de dar e quando quero um "Editar" ele diz-me que: "

The GridView 'GVDados' fired event RowUpdating which wasn't handled."

Que será que posso fazer?

E já agora devo ter demasiados "using....."

Este é o meu código:

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

namespace Estagio_Registos
{
public partial class Programas : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		Response.Cache.SetNoStore();
		if (Session["id"] == null)
		{
			Response.Redirect("~/Login.aspx");
		}

..............

		cnReg.Open();

		SqlDataAdapter da = new SqlDataAdapter("SELECT Codigo_Prog, Versao, Data, Observacoes FROM Programas_Linhas Where Codigo_Prog=" + Convert.ToInt32(txtCodProg.Text) + "", cnReg);
		DataTable dt = new DataTable();
		da.Fill(dt);

		DataRow dr = dt.NewRow();
		dt.Rows.InsertAt(dr, 0);
		GVDados.EditIndex = 0;
		GVDados.DataSource = dt;
		GVDados.DataBind();

		cnReg.Close();
	}
	SqlConnection cnReg = new SqlConnection("Data Source=LN\\Digisoft;Initial Catalog=Registos;Integrated Security=True");

	protected void btnAdVers_Click(object sender, EventArgs e)
	{
		SqlDataAdapter da = new SqlDataAdapter("SELECT Versao, Data, Observacoes FROM Programas_Linhas Where Codigo_Prog=" + Convert.ToInt32(txtCodProg.Text) + "", cnReg);
		DataTable dt = new DataTable();
		da.Fill(dt);

		DataRow dr = dt.NewRow();
		dt.Rows.InsertAt(dr, 0);
		GVDados.EditIndex = 0;
		GVDados.DataSource = dt;
		GVDados.DataBind();
	}


	protected void GVDados_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		if (((LinkButton)GVDados.Rows[0].Cells[0].Controls[0]).Text == "Insert")
		{
			SqlCommand cmd = new SqlCommand();

			cmd.CommandText = "INSERT INTO Programas_Linhas(Versao, Data, Observacoes) VALUES(@Versao, @Data, @Observacoes) Where Codigo_Prog=" + Convert.ToInt32(txtCodProg.Text) + "";

			cmd.Parameters.Add("@Versao", SqlDbType.VarChar).Value = ((TextBox)GVDados.Rows[0].Cells[1].Controls[0]).Text;
			cmd.Parameters.Add("@Data", SqlDbType.DateTime2).Value = ((TextBox)GVDados.Rows[0].Cells[2].Controls[0]).Text;
			cmd.Parameters.Add("@Observacoes", SqlDbType.VarChar).Value = ((TextBox)GVDados.Rows[0].Cells[3].Controls[0]).Text;

			cmd.Connection = cnReg;

			cnReg.Open();

			cmd.ExecuteNonQuery();

			cnReg.Close();
		}

		else
		{
			GVDados.EditIndex = e.RowIndex;
			DataBind();

			SqlCommand cmd = new SqlCommand();

			cmd.CommandText = "UPDATE Programas_Linhas SET Versao=@Versao, Data=@Data, Observacoes=@Observacoes WHERE Codigo_Prog=" + Convert.ToInt32(txtCodProg.Text) + "";

			cmd.Parameters.Add("@Versao", SqlDbType.VarChar).Value = Convert.ToString(GVDados.Rows[e.RowIndex].Cells[1].Text);
			cmd.Parameters.Add("@Data", SqlDbType.DateTime2).Value = Convert.ToDateTime(GVDados.Rows[e.RowIndex].Cells[2].Text);
			cmd.Parameters.Add("@Observacoes", SqlDbType.VarChar).Value = Convert.ToString(GVDados.Rows[e.RowIndex].Cells[3].Text);

			cmd.Connection = cnReg;

			cnReg.Open();

			cmd.ExecuteNonQuery();

			cnReg.Close();
		}

		GVDados.EditIndex = -1;
		DataBind();
	}
}
}
Posted (edited)

Qual é a opção???

Já eliminei os Usings desnecessarios. So tenho estes:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

Edited by LN10

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.