using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da;
DataTable dt;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView();
}
}
private void GridView()
{
da = new SqlDataAdapter("select * from test", "Data Source=SPADEWRO-47A76C;Initial Catalog=test;Integrated Security=True");
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str;
str = GridView1.Rows[e.RowIndex].Cells[2].Text;
SqlDataAdapter daa = new SqlDataAdapter("delete from test where No=" + str + " ", "Data Source=SPADEWRO-47A76C;Initial Catalog=test;Integrated Security=True");
ds = new DataSet();
daa.Fill(ds);
GridView1.EditIndex = -1;
GridView();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlDataAdapter daa ;
DataSet dss ;
TextBox t1, t2;
t1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]);
t2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]);
if(((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
{
daa = new SqlDataAdapter("insert into test values("+t1.Text+", '"+t2.Text+"')", "Data Source=SPADEWRO-47A76C;Initial Catalog=test;Integrated Security=True");
dss = new DataSet();
daa.Fill(dss);
GridView1.EditIndex = -1;
}
else
{
daa = new SqlDataAdapter("update test set name='" + t2.Text + "' where No=" + t1.Text + " ", "Data Source=SPADEWRO-47A76C;Initial Catalog=test;Integrated Security=True");
dss = new DataSet();
daa.Fill(dss);
}
GridView1.EditIndex = -1;
GridView();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataAdapter daa = new SqlDataAdapter("select * from test", "Data Source=SPADEWRO-47A76C;Initial Catalog=test;Integrated Security=True");
DataTable dtt = new DataTable ();
daa.Fill(dtt);
DataRow dr = dtt.NewRow();
dtt.Rows.InsertAt(dr, 0);
GridView1.EditIndex = 0;
GridView1.DataSource = dtt;
GridView1.DataBind();
((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
}
}
No comments:
Post a Comment