当数据来自数据库时,如何在gridview中编辑,更新和删除? [英] How to edit, update and delete in gridview when data comes from database?
问题描述
这个问题可以在这里多次提出,但我的情况有所不同。请原谅我,如果我错了,我有一个页面,当我在文本框中输入部门名称时,我有文本框和按钮,按下提交按钮我的数据库中的数据存储
现在我想要的是我想要
(1)当我按下提交按钮时,我可以在我的网格视图中看到该部门。
(2)在网格中显示我的部门名称,带有编辑和删除链接,这样当我点击编辑链接部门名称将弹出部门文本框并提交按钮成为更新按钮,当我进行一些更改并单击更新按钮,它将更新并显示在网格中。
我该怎么做这是我的部门page.cs代码:
This question can be ask here many time but my case is different. Please forgive me if i am wrong i have a page in which i have text-box and a button when i enter department name in text-box and press submit button my data store in the database
Now what i want is i want
(1) When i press submit button i can see that department in my grid-view.
(2) to show my department name in grid with edit and delete link so that when i click on edit link department name will pop in the department text-box and submit button become update button and when i make some changes and click on update button it will update and show it in the grid.
how can i do that This is my department page.cs code:
public partial class DepartmentMaster : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WebGallery"].ToString());
SqlDataAdapter da; DataTable dt = new DataTable();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
con.Open();
da = new SqlDataAdapter("Select * from Department_Master", con); da.Fill(ds);
gvDepartment.DataSource = ds;
gvDepartment.DataBind();
con.Close();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{ using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyGallery"].ToString()))
{
con.Open(); string DepartmentName = tbDepartment.Text.Trim();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "insert into Department_Master(DepartmentName) values('" + DepartmentName + "')";
cmd.Parameters.AddWithValue("@DepartmentName", tbDepartment.Text.Trim());
cmd.ExecuteNonQuery();
}
con.Close();
tbDepartment.Text = "";
tbDepartment.Focus();
}
}
}
推荐答案
读取这个......:)
http ://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html [ ^ ]
Read this....:)
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]
这篇关于当数据来自数据库时,如何在gridview中编辑,更新和删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!