Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > มีปัญหา Edit กับ Delete Data from Gridview ด้วย checkbox ครับ


 

[.NET] มีปัญหา Edit กับ Delete Data from Gridview ด้วย checkbox ครับ

 
Topic : 128144



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์



เวลา Compile แล้วในส่วนของ Edit ไม่สามารถแก้ไขข้อมูลได้ ส่วนของ Delete Error ว่า "Input string was not in a correct format." ขอบคุณครับ

โค้ต ASP ครับ
Code (ASP)
01.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EditUpdateDelete.WebForm1" %>
02. 
03.<!DOCTYPE html>
04. 
06.<head runat="server">
07.    <title></title>
08.</head>
09.<body>
10.    <form id="form1" runat="server">
11.        <div>
12.            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
13.                <Columns>
14.                    <asp:TemplateField>
15.                        <ItemTemplate>
16.                            <asp:CheckBox ID ="chkdel" runat ="server" />
17.                        </ItemTemplate>
18.                    </asp:TemplateField>
19.                    <asp:TemplateField HeaderText ="ID">
20.                        <ItemTemplate>
21.                            <asp:Label ID ="lb_ID" runat ="server" Text ='<%#Eval("ID") %>'></asp:Label>
22.                        </ItemTemplate>
23.                    </asp:TemplateField>
24.                    <asp:TemplateField HeaderText="Firstname">
25.                        <ItemTemplate>
26.                            <asp:Label ID="lb1" runat="server" Text='<%#Eval("fname") %>'></asp:Label>
27.                        </ItemTemplate>
28.                        <EditItemTemplate>
29.                            <asp:TextBox ID="txt1" runat="server" Text='<%#Eval("fname") %>'></asp:TextBox>
30.                        </EditItemTemplate>
31.                    </asp:TemplateField>
32.                    <asp:TemplateField HeaderText="Lastname">
33.                        <ItemTemplate>
34.                            <asp:Label ID="lb2" runat="server" Text='<%#Eval("lname") %>'></asp:Label>
35.                        </ItemTemplate>
36.                        <EditItemTemplate>
37.                            <asp:TextBox ID="txt2" runat="server" Text='<%#Eval("lname") %>'></asp:TextBox>
38.                        </EditItemTemplate>
39.                    </asp:TemplateField>
40.                    <asp:TemplateField>
41.                        <ItemTemplate>
42.                            <asp:Button ID ="btn1" runat ="server" Text ="Edit" CommandName ="Edit"/>
43.                        </ItemTemplate>
44.                        <EditItemTemplate>
45.                            <asp:Button ID ="btn2" runat ="server" Text ="Update" CommandName ="Edit" />
46.                            <asp:Button ID ="btn3" runat ="server" Text ="Cancel" CommandName ="Cancel" />
47.                        </EditItemTemplate>
48.                    </asp:TemplateField>
49.                </Columns>
50.                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
51.                <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
52.                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
53.                <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
54.                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
55.                <SortedAscendingCellStyle BackColor="#FFF1D4" />
56.                <SortedAscendingHeaderStyle BackColor="#B95C30" />
57.                <SortedDescendingCellStyle BackColor="#F1E5CE" />
58.                <SortedDescendingHeaderStyle BackColor="#93451F" />
59.            </asp:GridView><br />
60.            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
61.        </div>
62.    </form>
63.</body>
64.</html>


โค้ต c# ครับ
Code (C#)
001.using System;
002.using System.Collections.Generic;
003.using System.Linq;
004.using System.Web;
005.using System.Web.UI;
006.using System.Web.UI.WebControls;
007.using System.Data;
008.using System.Data.SqlClient;
009.using System.Web.Configuration;
010. 
011.namespace EditUpdateDelete
012.{
013.    public partial class WebForm1 : System.Web.UI.Page
014.    {
015.        protected void Page_Load(object sender, EventArgs e)
016.        {
017.            if (!IsPostBack)
018.            {
019.                ShowData();
020.            }
021.        }
022. 
023.        string strCon = WebConfigurationManager.ConnectionStrings["AdminRoleConnectionString"].ConnectionString;
024. 
025.        protected void ShowData()
026.        {
027.            SqlConnection con = new SqlConnection(strCon);
028.            con.Open();
029. 
030.            string sql = "select ID,fname,lname from AllUser";
031.            SqlDataAdapter cmd = new SqlDataAdapter(sql, con);
032.            DataSet ds = new DataSet();
033.            cmd.Fill(ds);
034. 
035.            GridView1.DataSource = ds;
036.            GridView1.DataBind();
037. 
038.            if (ds.Tables[0].Rows.Count > 0)
039.            {
040.                GridView1.DataSource = ds;
041.                GridView1.DataBind();
042.            }
043.            con.Close(); 
044.        }
045. 
046.        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
047.        {
048.            GridView1.EditIndex = e.NewEditIndex;
049.            ShowData();
050.        }
051. 
052.        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
053.        {
054.            GridView1.EditIndex = -1;
055.            ShowData();
056.        }
057. 
058.        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
059.        {
060.            Label ID = GridView1.Rows[e.RowIndex].FindControl("lb_ID") as Label;
061.            TextBox Firstname = GridView1.Rows[e.RowIndex].FindControl("txt1") as TextBox;
062.            TextBox Lastname = GridView1.Rows[e.RowIndex].FindControl("txt2") as TextBox;
063.             
064.            SqlConnection con = new SqlConnection(strCon);
065.            con.Open();
066. 
067.            string sql2 = "Update AllUser set fname='" + Firstname.Text + "', lname = '" + Lastname.Text + "' where ID =+ Convert.ToInt32(ID.Text)+";
068.            SqlCommand cmd = new SqlCommand(sql2, con);
069.            cmd.ExecuteNonQuery();
070.            con.Close();
071. 
072.            GridView1.EditIndex = -1;
073.            ShowData();
074.        }
075. 
076.        protected void DeleteRecord(int IDUser)
077.        {
078.            SqlConnection con = new SqlConnection(strCon);
079.            SqlCommand cmd = new SqlCommand("delete from AllUser where ID=@ID", con);
080.            cmd.Parameters.AddWithValue("@ID", IDUser);
081.            con.Open();
082.            cmd.ExecuteNonQuery();
083.            con.Close();
084.        }
085. 
086.        protected void Button1_Click(object sender, EventArgs e)
087.        {
088.            foreach (GridViewRow grow in GridView1.Rows)
089.            {
090.                //Searching CheckBox("chkDel") in an individual row of Grid
091.                CheckBox chkdel = (CheckBox)grow.FindControl("chkdel");
092.                //If CheckBox is checked than delete the record with particular empid
093.                if (chkdel.Checked)
094.                {
095.                    int IDUser = Convert.ToInt32(grow.Cells[1].Text);
096.                    DeleteRecord(IDUser);
097.                }
098.            }
099.        }
100.    }
101.}




Tag : .NET, MySQL, Web (ASP.NET), C#



ประวัติการแก้ไข
2017-06-29 16:15:07
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-06-29 16:12:22 By : kanin9182 View : 791 Reply : 0
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : มีปัญหา Edit กับ Delete Data from Gridview ด้วย checkbox ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่