本文介绍了ASP.NET - GridView控件与编程工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从这个帖子。
多的谷歌搜索后,我想出了这个code编程编辑单元格:
使用系统;
使用System.Data这;
使用System.Configuration;
System.Collections中使用;
使用的System.Web;
使用System.Web.Security;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;使用Ice_Web_Portal.BO;命名空间GridView___Test
{
公共部分类_Default:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
} 保护无效GridView1_RowEditing(对象发件人,GridViewEditEventArgs E)
{
GridViewRow行= GridView1.Rows [e.NewEditIndex] GridView1.EditIndex = e.NewEditIndex; GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
} 保护无效GridView1_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{
文本框txtID =(文本框)GridView1.Rows [e.RowIndex] .Cells [1] .Controls [0];
文本框txtCourse code =(文本框)GridView1.Rows [e.RowIndex] .Cells [2] .Controls [0];
文本框txtCourseName =(文本框)GridView1.Rows [e.RowIndex] .Cells [3] .Controls [0];
文本框txtCourseTextBook code =(文本框)GridView1.Rows [e.RowIndex] .Cells [4] .Controls [0]; 课程项目=新课程();
item.ID = Convert.ToInt32(txtID.Text);
item.Course code = txtCourse code.Text;
item.CourseName = txtCourseName.Text;
item.TextBook code = txtCourseTextBook code.Text; BOOL成功= Course.Update(项目); labMessage.Text = success.ToString(); GridView1.EditIndex = -1;
GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
}
}
}
但2个问题正在发生。
(1)我需要preSS命令按钮两次,以编辑/更新。
不更新在数据库中(2)在细胞中的值变化。即编辑单元格的值不会犯。
谁能给我一个解决方案?
更新:我的解决办法是这样的:
<%@页面语言=C#AutoEventWireup =真codeBehind =Default.aspx.cs继承=GridView的___ Test._Default%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>无标题页< /标题>
< /头><身体GT;
<表ID =form1的=服务器>
< DIV> < ASP:GridView控件ID =GridView1=服务器FONT-名称=宋体FONT-SIZE =小的AutoGenerateColumns =FALSEOnRowCancelingEdit =GridView1_RowCancelingEditOnRowCommand =GridView1_RowCommandOnRowEditing =GridView1_RowEditingOnRowUpdating =GridView1_RowUpdating>
< / ASP:GridView的> < / DIV>
< /表及GT;
< /身体GT;< / HTML>命名空间GridView___Test
{
公共部分类_Default:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
//如果(!Page.IsPostBack)
{
CreateGridView();
}
} 私人无效CreateGridView()
{
GridView1.Columns.Clear(); 。DataTable中的dataTable = Book.GetBooksDataSet()表[0]; CommandField中CF =新CommandField中();
cf.ShowEditButton = TRUE; GridView1.Columns.Add(CF); INT colCount = 1;
的foreach(在dataTable.Columns的DataColumn C)
{
绑定列绑定列=新的BoundField(); boundField.DataField = c.ColumnName;
boundField.HeaderText = c.ColumnName;
//boundField.FooterText =---; 如果(colCount == || 3 == colCount 5)
{
boundField.ReadOnly = TRUE;
} GridView1.Columns.Add(绑定列);
colCount ++;
} GridView1.ShowFooter = TRUE; GridView1.DataSource = dataTable的;
GridView1.DataBind(); GridViewRow footerRow = GridView1.FooterRow;
按钮B =新按钮();
b.Text =新增;
INT I = 0;
footerRow.Cells [I] .Controls.Add(二);
的foreach(在dataTable.Columns的DataColumn C)
{
++我;
文本框TB =新的TextBox();
footerRow.Cells [I] .Controls.Add(TB);
}
} 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
INT指数= Convert.ToInt32(e.CommandArgument); 如果(e.CommandName ==编辑)
{
//注意到GridView的编辑模式。
GridView1.EditIndex =指数; GridViewRow selectedRow =((GridView控件)e.CommandSource).Rows [指数] //我们可以得到这样的单元格数据
字符串ID = selectedRow.Cells [1]。文本;
字符串ISBN = selectedRow.Cells [2]。文本; //这是必要的GridView中露面。
CreateGridView();
}
否则,如果(e.CommandName ==更新)
{
LinkButton的updateButton =(LinkButton的)e.CommandSource; DataControlFieldCell DCFC =(DataControlFieldCell)updateButton.Parent; GridViewRow GVR =(GridViewRow)dcfc.Parent; //更新...................
//更新网格的数据数据库
UpdateDataInTheDatabase(gvr.Cells [1] .Controls); //电网恢复到正常的
GridView1.EditIndex = -1; //这是必要的GridView中露面。
CreateGridView();
}
} 私人无效UpdateDataInTheDatabase(ControlCollection中CC)
{
文本框TB =(文本框)CC [0];
// ...
// ...
// ... //这里调用Update持久性code ...
} #地区的应用满意的事件处理程序
保护无效GridView1_RowEditing(对象发件人,GridViewEditEventArgs E)
{
} 保护无效GridView1_RowCancelingEdit(对象发件人,GridViewCancelEditEventArgs E)
{
} 保护无效GridView1_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{
}
#endregion
}
}
解决方案
有关问题的#1,尽量
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(!Page.IsPostBack)
{
GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
}
}
有关问题的#2,我们需要检查你的方法 Course.Update(项目)
。
I am continuing from this post.
After much Googling, I have come up with this code to edit cells programmatically:
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 Ice_Web_Portal.BO;
namespace GridView___Test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewEditIndex];
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtID = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
TextBox txtCourseCode = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox txtCourseName = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox txtCourseTextBookCode = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
Course item = new Course();
item.ID = Convert.ToInt32(txtID.Text);
item.CourseCode = txtCourseCode.Text;
item.CourseName = txtCourseName.Text;
item.TextBookCode = txtCourseTextBookCode.Text;
bool success = Course.Update(item);
labMessage.Text = success.ToString();
GridView1.EditIndex = -1;
GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
}
}
}
But 2 problems are happening.
(1) I need to press command buttons twice to Edit/Update.
(2) Changes in the cell values are not updated in the database. I.e. edited cell values are not committing.
Can anyone give me a solution?
UPDATE: My solution was like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView___Test._Default" %>
<!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" Font-Names="Verdana" Font-Size="Small" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
</asp:GridView>
</div>
</form>
</body>
</html>
namespace GridView___Test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
{
CreateGridView();
}
}
private void CreateGridView()
{
GridView1.Columns.Clear();
DataTable dataTable = Book.GetBooksDataSet().Tables[0];
CommandField cf = new CommandField();
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
int colCount = 1;
foreach (DataColumn c in dataTable.Columns)
{
BoundField boundField = new BoundField();
boundField.DataField = c.ColumnName;
boundField.HeaderText = c.ColumnName;
//boundField.FooterText = "---";
if (colCount == 3 || colCount == 5)
{
boundField.ReadOnly = true;
}
GridView1.Columns.Add(boundField);
colCount++;
}
GridView1.ShowFooter = true;
GridView1.DataSource = dataTable;
GridView1.DataBind();
GridViewRow footerRow = GridView1.FooterRow;
Button b = new Button();
b.Text = "Add New";
int i = 0;
footerRow.Cells[i].Controls.Add(b);
foreach (DataColumn c in dataTable.Columns)
{
++i;
TextBox tb = new TextBox();
footerRow.Cells[i].Controls.Add(tb);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Edit")
{
//Takes the GridView to Edit mode.
GridView1.EditIndex = index;
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
//We can get cell data like this
string id = selectedRow.Cells[1].Text;
string isbn = selectedRow.Cells[2].Text;
//This is necessary to GridView to be showed up.
CreateGridView();
}
else if (e.CommandName == "Update")
{
LinkButton updateButton = (LinkButton)e.CommandSource;
DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
GridViewRow gvr = (GridViewRow)dcfc.Parent;
//The update...................
//Update grid-data to database
UpdateDataInTheDatabase(gvr.Cells[1].Controls);
//Grid goes back to normal
GridView1.EditIndex = -1;
//This is necessary to GridView to be showed up.
CreateGridView();
}
}
private void UpdateDataInTheDatabase(ControlCollection cc)
{
TextBox tb = (TextBox)cc[0];
//...
//...
//...
//Call the update persistance code here...
}
#region Application Satisfactory Event Handlers
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
#endregion
}
}
解决方案
for problem #1,try
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
GridView1.DataSource = Course.GetCourses();
GridView1.DataBind();
}
}
for problem #2, we need to check your method Course.Update(item)
.
这篇关于ASP.NET - GridView控件与编程工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!