using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.SqlClient;
using System.Web.Configuration;
public partial class datagrid : System.Web.UI.Page
{
SqlConnection conn;
SqlCommand com;
string strsql;
protected void Page_Load(object sender, EventArgs e)
{
string str;
str = WebConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
conn = new SqlConnection(str);
conn.Open();
if (!Page.IsPostBack)
{
BindData();
}
}
protected void BindData()
{
strsql = "select * From tbl_customer";
SqlDataReader dtReader;
com = new SqlCommand(strsql, conn);
dtReader = com.ExecuteReader();
myGridView.DataSource = dtReader;
myGridView.DataBind();
dtReader.Close();
dtReader = null;
}
protected void Page_Unload()
{
conn.Close();
conn = null;
}
protected void modEditCommand(object sender, GridViewEditEventArgs e)
{
myGridView.EditIndex = e.NewEditIndex;
myGridView.ShowFooter = false;
BindData();
}
protected void modCancelCommand(object sender, GridViewCancelEditEventArgs e)
{
myGridView.EditIndex = -1;
myGridView.ShowFooter = true;
BindData();
}
protected void modDeleteCommand(object sender, GridViewDeleteEventArgs e)
{
strsql = "Delete form tbl_customer WHERE ID= '" + myGridView.DataKeys[e.RowIndex].Value + "'";
com = new SqlCommand(strsql, conn);
com.ExecuteNonQuery();
myGridView.EditIndex = -1;
BindData();
}
protected void modUpdateCommand(object sender, GridViewUpdatedEventArgs e)
{
TextBox txtname = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditname");
TextBox txtlastname = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditlastname");
strsql = "UPDATE tbl_customer SET name='" + txtname.Text + "'" +
", lastname='" + txtlastname.Text + "'" +
"WHERE ID='" + myGridView.DataKeys[e.RowIndex].Value + "'";
com = new SqlCommand(strsql, conn);
com.ExecuteNonQuery();
myGridView.EditIndex = -1;
myGridView.ShowFooter = true;
BindData();
}
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
TextBox txtname = (TextBox)myGridView.FooterRow.FindControl("txtAddname");
TextBox txtlastname = (TextBox)myGridView.FooterRow.FindControl("txtAddlastname");
strsql = "INSERT INTO tbl_customer (name, lastname)" +
"values ('" + txtname.Text + "', '" + txtlastname.Text + "')";
com = new SqlCommand(strsql, conn);
com.ExecuteNonQuery();
BindData();
}
}
}
มันแจ้งerror แบบนี้ครับ
Error 1 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' could be found (are you missing a using directive or an assembly reference?) D:\ASP\testUpdate\datagrid.aspx.cs 71 54 D:\ASP\testUpdate\
Error 2 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' could be found (are you missing a using directive or an assembly reference?) D:\ASP\testUpdate\datagrid.aspx.cs 72 58 D:\ASP\testUpdate\
Error 3 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' could be found (are you missing a using directive or an assembly reference?) D:\ASP\testUpdate\datagrid.aspx.cs 75 50 D:\ASP\testUpdate\
Error 4 No overload for 'modUpdateCommand' matches delegate 'System.Web.UI.WebControls.GridViewUpdateEventHandler' D:\ASP\testUpdate\datagrid.aspx 1 1 D:\ASP\testUpdate\