如何在gride视图中插入,编辑,删除和更新数据 [英] how to insert,edit,delete and update data in gride view
本文介绍了如何在gride视图中插入,编辑,删除和更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下
My code is as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class gvdemo : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=upanshu-pc\sqlexpress;Initial Catalog=test;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddetails();
}
}
protected void binddetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Customer", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
display.Text = "No Record found....";
}
}
protected void grideview1_cancelingedit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
binddetails();
}
protected void grideview1_onrowcommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtname = (TextBox)GridView1.FooterRow.FindControl("TextBox1");
TextBox txtcity = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
TextBox txtmobile = (TextBox)GridView1.FooterRow.FindControl("TextBox6");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Customer(CustName,CustCity,Custmobile) values('" + txtname.Text + "','" + txtcity.Text + "','" + txtmobile.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
binddetails();
display.Text = "Record Inserted succesfully...";
}
else
{
display.Text = "record Not Inserted,,,,";
}
}
}
protected void grideview1_ondeleting(object sender, GridViewDeleteEventArgs e)
{
int custid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["CustId"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("delete from Customer where CustId=" + custid, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
binddetails();
display.Text = "Record Deleted Succesfully...";
}
}
protected void gridview1_onediting(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
binddetails();
}
protected void grideview1_onrowupdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["CustId"].ToString());
TextBox txtcity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox txtmobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
con.Open();
SqlCommand cmd = new SqlCommand("update Customer set CustCity='"+txtcity.Text+"',Custmobile='"+txtmobile.Text+"'",con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
binddetails();
display.Text = "Record Updated Succesfully...";
}
}
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" ShowFooter="True" DataKeyNames="CustId,CustName"
AutoGenerateColumns="False" onrowcancelingedit="grideview1_cancelingedit"
onrowcommand="grideview1_onrowcommand" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
onrowdeleting="grideview1_ondeleting" onrowediting="gridview1_onediting"
onrowupdating="grideview1_onrowupdating">
<columns>
<asp:TemplateField HeaderText="Command">
<edititemtemplate>
<asp:Button ID="Button1" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="Button2" runat="server" Text="Cancel" CommandName="Cancel" />
</edititemtemplate>
<itemtemplate>
<asp:Button ID="Button3" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button4" runat="server" Text="Delete" CommandName="Delete" />
</itemtemplate>
<footertemplate>
<asp:Button ID="Button5" runat="server" Text="AddNew" CommandName="AddNew" />
<asp:Button ID="Button6" runat="server" Text="Button" CommandName="Insert" />
</footertemplate>
<asp:TemplateField HeaderText="Customer Name">
<edititemtemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("CustName") %>'>
</edititemtemplate>
<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("CustName") %>'>
</itemtemplate>
<footertemplate>
<asp:TextBox ID="TextBox1" runat="server">
</footertemplate>
<asp:TemplateField HeaderText="Customer City">
<edititemtemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("CustCity") %>'>
</edititemtemplate>
<itemtemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("CustCity") %>'>
</itemtemplate>
<footertemplate>
<asp:TextBox ID="TextBox4" runat="server">
</footertemplate>
<asp:TemplateField HeaderText="Mobile No.">
<edititemtemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("Custmobile") %>'>
</edititemtemplate>
<itemtemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("Custmobile") %>'>
</itemtemplate>
<footertemplate>
<asp:TextBox ID="TextBox6" runat="server">
</footertemplate>
</columns>
<footerstyle backcolor="#99CCCC" forecolor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<pagerstyle backcolor="#99CCCC" forecolor="#003399" horizontalalign="Left" />
<rowstyle backcolor="White" forecolor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<sortedascendingcellstyle backcolor="#EDF6F6" />
<sortedascendingheaderstyle backcolor="#0D4AC4" />
<sorteddescendingcellstyle backcolor="#D6DFDF" />
<sorteddescendingheaderstyle backcolor="#002876" />
<asp:Label ID="display" runat="server" Text="">
</div>
</form>
推荐答案
check
http://www.aspdotnet-suresh.com/2015/07/aspnet-gridview-crud-operations-insert-select-edit-update-delete-using-single-stored-procedure -example.html[^]
这篇关于如何在gride视图中插入,编辑,删除和更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文