DATA GRID VIEW - ASPX : "Aoutoid"

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

DATA GRID VIEW-------------------- ASPX;

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="data grid view.aspx.cs"


Inherits="data_grid_view" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 546px; background-color: #990033;">
<asp:GridView ID="GridView1" runat="server" ShowFooter="true"
AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting1"
OnRowCancelingEdit="GridView1_RowCancelingEdit" Caption="DETAILS" CaptionAlign="Top"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowCommand="GridView1_RowCommand" CellSpacing="2" CellPadding="2" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("aoutoid")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Labelidhg" runat="server" Text='<%#Eval("aoutoid")
%>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("name")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("name")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("name")
%>'></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("address")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("address")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>

<asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("address")


%>'></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("phone")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("phone")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%#Eval("phone")
%>'></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label_city" runat="server" Text='<%#Eval("city")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox_city" runat="server" Text='<%#Eval("city")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox_city1" runat="server" Text='<%#Eval("city")
%>'></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="edit">edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server"
CommandName="delete">delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnk_update" runat="server"
CommandName="update">update</asp:LinkButton>
<asp:LinkButton ID="lnk_cancel" runat="server"
CommandName="cancel">cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton3" runat="server"
CommandName="insert">insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#FF9933" HorizontalAlign="Justify" />
</asp:GridView>
</div>
</form>
</body>
</html>

CS CODE-------------------------using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;
System.IO;
System.Data;
System.Data.SqlClient;
System.Data.SqlTypes;

public partial class data_grid_view : System.Web.UI.Page


{
SqlConnection conection = new SqlConnection(@"Data Source=AINS\TRAIN;Initial
Catalog=biz;User ID=sa;Password=train123;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
view();
}
}
protected void view()
{
SqlDataAdapter sda = new SqlDataAdapter("select * from tbl_view", conection);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
view();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
conection.Open();
TextBox txt_name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
TextBox txt_address = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
TextBox txt_phone = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
TextBox txt_city = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox_city");
Label lblln = (Label)GridView1.Rows[e.RowIndex].FindControl("Labelidhg");
SqlCommand cmd = new SqlCommand("update tbl_view set name='" +txt_name.Text+
"',address='"+txt_address.Text+"',phone='"+txt_phone.Text+"',city='"+txt_city.Text+"' where
aoutoid='"+ lblln.Text+"'", conection);

cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
view();
conection.Close();
}
protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
{
conection.Open();
Label dynamiclbl = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
SqlCommand cmd2 = new SqlCommand("delete from tbl_view where aoutoid='" +
dynamiclbl.Text + "'", conection);
cmd2.ExecuteNonQuery();
GridView1.EditIndex = -1;
view();
conection.Close();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
view();

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void insert(string fname,string name)
{

}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "insert")
{
TextBox txt_name = (TextBox)GridView1.FooterRow.FindControl("TextBox2");
TextBox txt_address = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
TextBox txt_phone = (TextBox)GridView1.FooterRow.FindControl("TextBox6");
TextBox txt_city = (TextBox)GridView1.FooterRow.FindControl("TextBox_city1");
conection.Open();
SqlCommand cmd = new SqlCommand("insert into tbl_view(name,address,phone,city)
values('" + txt_name.Text + "','" + txt_address.Text + "','" + txt_phone.Text + "','" +
txt_city.Text + "')", conection);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
view();
}
}

You might also like