Gridview Control ไม่สามารถ Delete Update ได้ค่ะ รบกวนพี่ๆช่วยดูโค้ดให้ทีค่ะ
รบกวนดูโค้ดให้ทีค่ะ ทำตามในบทเรียนแล้วแต่ก็ยังไม่ได้
https://www.thaicreate.com/asp.net/c-sharp-asp.net-gridview-control-dropdownlist.html
คือมันแปลกๆตรงที่ว่า GridView1.Rows[e.RowIndex].FindControl("txtdate") ตรง e.RowIndex ตอนที่พิมมันไม่ขึ้นมาให้ค่ะ
แล้วพอ debug มันขึ้น error ว่า
Code
'System.Web.UI.WebControls.GridViewDeletedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewDeletedEventArgs' could be found (are you missing a using directive or an assembly reference?)
Code (C#)
protected void GridViewBindData()
{
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["InsuranceConnectionString"].ConnectionString);
Conn.Open();
string str = "SELECT i.InsuranceF_id,i.Form_id,i.credit,i.Customer_id,i.date,i.Employee_id,f.Form_id,f.formname,c.Customer_id,c.name,c.surname FROM InsuranceF i,Customer c,Form f WHERE i.Customer_id = c.Customer_id AND i.Form_id=f.Form_id AND i.Employee_id ='" + Session["Employee_id"] + "' AND name LIKE '%" + Keyword + "%'";
SqlDataReader dtReader;
SqlCommand command = new SqlCommand(str, Conn);
dtReader = command.ExecuteReader();
GridView1.DataSource = dtReader;
GridView1.DataBind();
dtReader.Close();
dtReader = null;
Conn.Close();
}
public DataTable DataTableFormname()
{
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["InsuranceConnectionString"].ConnectionString);
Conn.Open();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataTable dt = new DataTable();
string str = "SELECT * FROM Form";
dtAdapter = new SqlDataAdapter(str, Conn);
dtAdapter.Fill(dt);
dtAdapter = null;
return dt;
Conn.Close();
}
protected void modEditCommand(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBindData();
}
protected void modCancelCommand(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBindData();
}
protected void modDeleteCommnad(object sender, GridViewDeletedEventArgs e)
{
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["InsuranceConnectionString"].ConnectionString);
Conn.Open();
string str = "DELETE FROM InsuranceF WHERE InsuranceF_id ='" + GridView1.DataKeys[e.RowIndex].Value + "'";
SqlCommand command = new SqlCommand(str, Conn);
command.ExecuteNonQuery();
GridView1.EditIndex = -1;
GridViewBindData();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtdate = (TextBox)e.Row.FindControl("txtdate");
if (txtdate != null)
{
txtdate.Text = (string)DataBinder.Eval(e.Row.DataItem, "date");
}
TextBox txtcredit = (TextBox)e.Row.FindControl("txtcredit");
if (txtcredit != null)
{
txtcredit.Text = (string)DataBinder.Eval(e.Row.DataItem, "credit");
}
DropDownList ddlformname = (DropDownList)e.Row.FindControl("ddlformname");
if (ddlformname != null)
{
ddlformname.DataSource = (DataTable)DataTableFormname();
ddlformname.DataTextField = "formname";
ddlformname.DataValueField = "Form_id";
ddlformname.DataBind();
ddlformname.Items.IndexOf(ddlformname.Items.FindByValue((string)DataBinder.Eval(e.Row.DataItem, "Form_id")));
}
}
}
protected void modUpdateCommand(Object sender, GridViewUpdatedEventArgs e)
{
TextBox txtdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");
TextBox txtcredit = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcredit");
DropDownList ddlformname = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlformname");
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["InsuranceConnectionString"].ConnectionString);
Conn.Open();
string str = "UPDATE InsuranceF SET credit = '" + txtcredit.Text + "',date = '" + txtdate.Text + "',Form_id = '" + ddlformname.SelectedItem.Value + "' WHERE InsuranceF_id ='" + GridView1.DataKeys[e.RowIndex].Value + "'";
SqlCommand command = new SqlCommand(str, Conn);
command.ExecuteNonQuery();
GridView1.EditIndex = -1;
GridViewBindData();
}
<!--gridview-->
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="InsuranceF_id"
onRowDataBound="GridView1_RowDataBound"
OnRowEditing="modEditCommand"
OnRowCancelingEdit="modCancelCommand"
OnRowDeleting="modDeleteCommand"
OnRowUpdating="modUpdateCommnad">
<Columns>
<asp:TemplateField HeaderText="InsuranceF_id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblInsuranceF_id" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.InsuranceF_id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblInsuranceF_id" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.InsuranceF_id") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ชื่อ">
<ItemTemplate>
<asp:Label ID="name" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.name") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="นามสกุล">
<ItemTemplate>
<asp:Label ID="surname" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.surname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblsurname" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.surname") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="แบบประกัน">
<ItemTemplate>
<asp:Label ID="formname" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.formname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlformname" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ยอดเงิน">
<ItemTemplate>
<asp:Label ID="credit" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.credit") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtcredit" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="วันที่ขาย">
<ItemTemplate>
<asp:Label ID="date" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.date") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdate" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="" ShowEditButton="true" CancelText="Cancel" DeleteText="Delete" EditText="Edit" UpdateText="Update" />
<asp:CommandField ShowDeleteButton="true" HeaderText="" />
</Columns>
</asp:GridView>
<!--endgridview-->
Tag : ASP.NET, MySQL, Web (ASP.NET), C#
Date :
2010-09-27 01:39:59
By :
monchii
View :
1625
Reply :
3
ตอนแฮนเดิลก็แฮนเดิลด้วย OnRowDeleting แต่ EditEventArgs ดันใช้ RowDeleted ทำไมไม่เข้าใจ
protected void modCancelCommand(object sender, GridViewCancelEditEventArgs e)
แก้เป็น
protected void modCancelCommand(object sender, GridViewDeleteEventArgs e)
Date :
2010-09-27 08:28:42
By :
tungman
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Web.Configuration;
using System.Data.SqlClient;
public partial class Library_Library_User_MasterPage : System.Web.UI.MasterPage
{
private string strConn = WebConfigurationManager.ConnectionStrings["Library"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
{
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
string sql = "SELECT member_id, password, name, right_id, FROM UserSystem WHERE member_id = '" + TextBox1.Text + "' AND password = '" + TextBox2.Text + "'";
SqlCommand cmd = new SqlCommand(sql, Conn);
SqlDataReader user = cmd.ExecuteReader();
if (user.Read())
{
Session["member_id"] = user["member_id"].ToString();
Session["password"] = user["password"].ToString();
Session["name"] = user["name"].ToString();
Session["right_id"] = user["right_id"].ToString();
if (Session["right_id"].ToString() == "1")
{
Conn.Close();
Response.Redirect("~/studen/studenIndex.aspx");
}
else if (Session["right_id"].ToString() == "2")
{
Conn.Close();
Response.Redirect("~/techer/techerIndex.aspx");
}
else if (Session["right_id"].ToString() == "3")
{
Conn.Close();
Response.Redirect("~/bannarak/bannarakIndex.aspx");
}
else if (Session["right_id"].ToString() == "4")
{
Conn.Close();
Response.Redirect("~/Boss/BossIndex.aspx");
}
else if (Session["right_id"].ToString() == "5")
{
Conn.Close();
Response.Redirect("~/Addmin/AddminIndex.aspx");
}
}
}
}
}
Date :
2011-02-02 21:53:46
By :
nu
Load balance : Server 04