Jump to content

Adicionar Dados numa GV e carregar Dados de uma BD pa uma GV


LN10

Recommended Posts

Bom dia.

Gostaria que a minha GridView(GV) carrega-se automaticamente com os Dados da BD de acordo com o Código da TextBox.

Ao clicar no Botão "Adicionar Versões", ele inseri-se os Dados de uma linha previamente inserida ao fazer o Load da página e ao clicar no Botão "Editar", ser possível editar os Dados previamente inseridos.

Este é o meu código:

C#
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;

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");
		}

		txtCodProg.Attributes.Add("onkeypress", "return isNumberKey(event)");
		txtLicencas.Attributes.Add("onkeypress", "return isNumberKey(event)");
		txtPreco.Attributes.Add("onkeypress", "return isNumberKey(event)");
		txtActualizacao.Attributes.Add("onkeypress", "return isNumberKey(event)");

txtCodProg.Text = "1";

		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 btnCancelar_Click(object sender, EventArgs e)
	{
		Response.Redirect("Area_Gest.aspx");
	}

	private bool TabelaVazia(string pTabela, string pCampo)
	{
		cnReg.Open();

		bool pReturn = true;
		SqlCommand mSQL = new SqlCommand("Select COUNT(" + pCampo + ") from " + pTabela + "", cnReg);
		SqlDataReader exec = mSQL.ExecuteReader();

		exec.Read();
		int reg = exec.GetInt32(0);

		exec.Close();
		cnReg.Close();

		if (reg > 0)
		{ pReturn = false; }

		return pReturn;
	}

	protected void btnAdVers_Click(object sender, EventArgs e)
	{
		DataTable dt = new DataTable();
		dt = (DataTable)GVDados.DataSource;
		DataRow dr = dt.NewRow();
		dt.Rows.InsertAt(dr, 0);
		GVDados.DataSource = dt;
	}

	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[0].Text);
			cmd.Parameters.Add("@Data", SqlDbType.DateTime).Value = Convert.ToDateTime(GVDados.Rows[e.RowIndex].Cells[0].Text);
			cmd.Parameters.Add("@Observacoes", SqlDbType.VarChar).Value = Convert.ToString(GVDados.Rows[e.RowIndex].Cells[0].Text);

			cmd.Connection = cnReg;

			cnReg.Open();

			cmd.ExecuteNonQuery();

			cnReg.Close();
		}
		GVDados.EditIndex = -1;
		DataBind();
	}
}
}

ASP.NET
<asp:GridView
	ID="GVDados" runat="server" align="Center" AutoGenerateColumns="False" onrowupdating="GVDados_RowUpdating">
	<Columns>
		<asp:CommandField CancelText="" DeleteText="" EditText="Editar"
			ShowEditButton="True" />
	</Columns>
</asp:GridView>

<asp:Button ID="btnAdVers" runat="server" Text="Adicionar Versões"
	onclick="btnAdVers_Click" />
Link to comment
Share on other sites

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.