LN10 Posted June 4, 2012 at 09:13 AM Report Share #460185 Posted June 4, 2012 at 09:13 AM 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 More sharing options...
renafi Posted June 4, 2012 at 06:00 PM Report Share #460366 Posted June 4, 2012 at 06:00 PM E qual é o problema que estás a ter?? Oracle Certified Professional - AdministraçãoOracle Certified Professional - Pl/sqlMCPD - Microsoft Certified Professional DeveloperMCTS - Microsoft Certified Technology Specialist Link to comment Share on other sites More sharing options...
LN10 Posted June 5, 2012 at 08:29 AM Author Report Share #460458 Posted June 5, 2012 at 08:29 AM O botão "Adicionar" não dá, o botão "Editar/Update" não dá Dispara a dizer que os Handlers nao tao disponiveis, algo assim do genero e os botoes nao fazem as suas funções e ao fazer o load, os dados da BD não aparecem na Grid. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now