 |
|
GridView ผมมันผิดตรไหนครับ คือผมได้ลองศึกษาโค๊ตในบทเรียนจากหัวข้อนี้ครับ |
|
 |
|
|
 |
 |
|
คือผมได้ลองศึกษาโค๊ตในบทเรียนจากหัวข้อนี้ครับ https://www.thaicreate.com/asp.net/c-sharp-asp.net-gridview-control-sql-server-2000-2005.html แต่มันยังติด error ตรงส่วนของ[e.RowIndex] ไม่ทราบว่าผมต้องแก้ไขปัญหานี้ยังไงครับ
Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datagrid.aspx.cs" Inherits="datagrid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False"
ShowFooter="True"
DataKeyNames="CustomerID"
OnRowCancelingEdit="modCancelCommand"
OnRowCommand="myGridView_RowCommand"
OnRowDeleting="modDeleteCommand"
OnRowEditing="modEditCommand"
OnRowUpdating="modUpdateCommand">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>' />
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddname" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="lastname">
<ItemTemplate>
<asp:Label ID="lbllastname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.lastname") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditlastname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.lastname") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddlastname" runat="server" />
<asp:Button ID="btnAdd" runat="server" Text="Add" CommandName="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" CancelText="Cancel" DeleteText="Delete" EditText="Edit" UpdateText="Update" HeaderText="Modify" />
<asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code (C#)
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\
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2009-11-30 10:53:21 |
By :
cyberwizard |
View :
2122 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดู Version ของ Framework ด้วยครับ
|
 |
 |
 |
 |
Date :
2009-11-30 11:31:40 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ VS2008 framework 3.5ครับพี่วิน
|
 |
 |
 |
 |
Date :
2009-11-30 12:41:39 |
By :
cyberwizard |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ท่านใดก็ได้ช่วยผมหน่อยนะครับ
|
 |
 |
 |
 |
Date :
2009-12-02 09:41:59 |
By :
cyberwizard |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำแค่นี้ ตรวจสอบดูก่อน ยังไม่ต้องยุ่งกับ database
ถ้าผ่านค่อยให้มัน update ลง db
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datagrid.aspx.cs" Inherits="datagrid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView id="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs
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 sqlConnection;
protected void Page_Load(object sender, EventArgs e)
{
string sqlConnectionString = WebConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
sqlConnection = new SqlConnection(sqlConnectionString);
GridView1.DataSource = BindData();
GridView1.AutoGenerateEditButton = true;
GridView1.DataKeyNames = new String[] { "CustomerID" };
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
GridView1.DataBind();
}
protected DataTable BindData()
{
string sqlCommandString = "select * From tbl_customer";
SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
DataTable Dt = new DataTable();
SqlDataAdapter DataAdapter = new SqlDataAdapter(sqlCommand);
DataAdapter.Fill(Dt);
return Dt;
}
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
}
|
 |
 |
 |
 |
Date :
2009-12-03 13:58:28 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|