DataGridView使用哪个方法或事件来更新与他绑定的DataTable 如题,谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我是用程序对dgv中的元素进行修改如果直接更新dgv没有办法更新到dataTable中随便点个dgv中的别的地方就可以更新dataTable ?? 你操作完DGV后,没有进行 validating 验证吧……你直接从UI操作的就可以,因为UI的操作会自动触发VALIDATING事件和 VALIDATED事件,之后便会将更改更新到后台的DT中。如果你是用代码自己操作的话,你要自己去调用validate方法。 http://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.validating.aspx 这个问题 问的大了 DataGridView的所有事件只要你写想关代码 都能更新DataTable 具体怎么操作就触发不同的事件来更新DataTable 会自动更新的,更新的时候会触发CellValueChange事件,好像是这个名字 在所有要再次执行的代码前面声明DataGridView DataGridView = new DataGridView();试试看 页面代码 <asp:GridView ID="gdvAccounts_Bank" runat="server" AllowSorting="True" EmptyDataText="<div align=center><font color=red><br/><br/><B>没有找到相关记录</B><br/><br/></font></div>" GridLines="None" Width="100%" HorizontalAlign="Center" AutoGenerateColumns="False" OnRowUpdating="gdvAccounts_Bank_RowUpdating" OnRowCancelingEdit="gdvAccounts_Bank_RowCancelingEdit" OnRowEditing="gdvAccounts_Bank_Editing" OnSorting="gdvAccounts_Bank_Sorting" OnRowDataBound="gdvAccounts_Bank_RowDataBound" OnRowDeleting="gdvAccounts_Bank_del" DataKeyNames="Id">后台代码//绑定数据源方法 private void bind() { //获取数据源 DataTable table = Account_BankManager.GetAccount_BanksTable(); this.AspNetPager1.RecordCount = table.Rows.Count; //每页的第一条数据的序号 ViewState["FirstData"] = (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize; //调用分页方法 PagedDataSource pageDate = Pages.GetDataSource(table, this.AspNetPager1, (string)ViewState["SortOrder"], (string)ViewState["OrderDire"], Convert.ToString(ViewState["Where"])); this.gdvAccounts_Bank.DataSource = pageDate; this.gdvAccounts_Bank.DataBind(); } #region 数据显示控件事件 //数据绑定后激发 protected void gdvAccounts_Bank_RowDataBound(object sender, GridViewRowEventArgs e) { //使序号列有序的显示 if (e.Row.RowIndex != -1) { //使下一页的序号紧跟着上一页的序号 ViewState["FirstData"] = Convert.ToInt32(ViewState["FirstData"]) + 1; e.Row.Cells[0].Text = ViewState["FirstData"].ToString(); } //如果是绑定数据行 if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) { ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这条记录吗?\\n\\n同时删除其它相关信息!')"); } } } //修改事件 protected void gdvAccounts_Bank_Editing(object sender, GridViewEditEventArgs e) { this.gdvAccounts_Bank.EditIndex = e.NewEditIndex; this.bind(); } //更新事件 protected void gdvAccounts_Bank_RowUpdating(object sender, GridViewUpdateEventArgs e) { //银行信息编号 int id = Convert.ToInt32(this.gdvAccounts_Bank.DataKeys[e.RowIndex].Value.ToString()); //银行名称 TextBox txtBankName = this.gdvAccounts_Bank.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox; string bankName = txtBankName.Text.Trim().ToString(); //银行金额 TextBox txtAmount = this.gdvAccounts_Bank.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox; decimal amount = 0; try { amount = Convert.ToDecimal(txtAmount.Text.Trim().ToString()); } catch (Exception) { this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入数字!');</script>"); } //银行备注 TextBox txtRes = this.gdvAccounts_Bank.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox; string res = txtRes.Text.Trim().ToString(); //调用改银行信息方法 Account_BankManager.ModifyAccount_Bank(id, bankName, amount, res); this.gdvAccounts_Bank.EditIndex = -1; this.bind(); } //取消修改事件 protected void gdvAccounts_Bank_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { this.gdvAccounts_Bank.EditIndex = -1; this.bind(); } //删除事件 protected void gdvAccounts_Bank_del(object sender, GridViewDeleteEventArgs e) { //获得要删除的编号 int id = Convert.ToInt32(this.gdvAccounts_Bank.DataKeys[e.RowIndex].Value.ToString()); //调用删除方法 Account_BankManager.DeleteAccount_BankByID(id); this.bind(); } //字段排序事件 protected void gdvAccounts_Bank_Sorting(object sender, GridViewSortEventArgs e) { string sPage = e.SortExpression; if (ViewState["SortOrder"].ToString() == sPage) { if (ViewState["OrderDire"].ToString() == "Desc") ViewState["OrderDire"] = "ASC"; else ViewState["OrderDire"] = "Desc"; } else { ViewState["SortOrder"] = e.SortExpression; } this.bind(); } #endregion不知行不行 C#调用动态链接库的结构体数组指针问题 用C#能否实现网站的自动登陆?具体怎么实现?谢谢! 关于WCF上传下载文件的问题 在.NET下如何用代码实现锁定屏幕 C语言新手 请大家看看这段代码错在哪里了 谢谢了 smartphone 2003 不支持button,radiobutton等控件吗? 名称“strlen”在类或命名空间“c.b1”中不存在 为什么类的static方法只能操作该类的static变量? 请教关于DATAGRID的问题。 在C#中怎么把一个Excel保存成为一个Html文件? one question 取出dataset中的某些数据
如果直接更新
dgv没有办法更新到dataTable中随便点个dgv中的别的地方
就可以更新dataTable
?? 你操作完DGV后,没有进行 validating 验证吧……你直接从UI操作的就可以,因为UI的操作会自动触发VALIDATING事件和 VALIDATED事件,之后便会将更改更新到后台的DT中。如果你是用代码自己操作的话,你要自己去调用validate方法。
DataGridView DataGridView = new DataGridView();试试看
<asp:GridView ID="gdvAccounts_Bank" runat="server" AllowSorting="True" EmptyDataText="<div align=center><font color=red><br/><br/><B>没有找到相关记录</B><br/><br/></font></div>"
GridLines="None" Width="100%" HorizontalAlign="Center" AutoGenerateColumns="False"
OnRowUpdating="gdvAccounts_Bank_RowUpdating" OnRowCancelingEdit="gdvAccounts_Bank_RowCancelingEdit"
OnRowEditing="gdvAccounts_Bank_Editing" OnSorting="gdvAccounts_Bank_Sorting"
OnRowDataBound="gdvAccounts_Bank_RowDataBound" OnRowDeleting="gdvAccounts_Bank_del"
DataKeyNames="Id">后台代码//绑定数据源方法
private void bind()
{
//获取数据源
DataTable table = Account_BankManager.GetAccount_BanksTable();
this.AspNetPager1.RecordCount = table.Rows.Count; //每页的第一条数据的序号
ViewState["FirstData"] = (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize; //调用分页方法
PagedDataSource pageDate = Pages.GetDataSource(table, this.AspNetPager1, (string)ViewState["SortOrder"], (string)ViewState["OrderDire"], Convert.ToString(ViewState["Where"]));
this.gdvAccounts_Bank.DataSource = pageDate;
this.gdvAccounts_Bank.DataBind();
}
#region 数据显示控件事件 //数据绑定后激发
protected void gdvAccounts_Bank_RowDataBound(object sender, GridViewRowEventArgs e)
{
//使序号列有序的显示
if (e.Row.RowIndex != -1)
{
//使下一页的序号紧跟着上一页的序号
ViewState["FirstData"] = Convert.ToInt32(ViewState["FirstData"]) + 1; e.Row.Cells[0].Text = ViewState["FirstData"].ToString();
} //如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick",
"javascript:return confirm('你确认要删除这条记录吗?\\n\\n同时删除其它相关信息!')");
}
}
} //修改事件
protected void gdvAccounts_Bank_Editing(object sender, GridViewEditEventArgs e)
{
this.gdvAccounts_Bank.EditIndex = e.NewEditIndex;
this.bind();
} //更新事件
protected void gdvAccounts_Bank_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//银行信息编号
int id = Convert.ToInt32(this.gdvAccounts_Bank.DataKeys[e.RowIndex].Value.ToString()); //银行名称
TextBox txtBankName = this.gdvAccounts_Bank.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
string bankName = txtBankName.Text.Trim().ToString(); //银行金额
TextBox txtAmount = this.gdvAccounts_Bank.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
decimal amount = 0;
try
{
amount = Convert.ToDecimal(txtAmount.Text.Trim().ToString());
}
catch (Exception)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入数字!');</script>");
} //银行备注
TextBox txtRes = this.gdvAccounts_Bank.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
string res = txtRes.Text.Trim().ToString(); //调用改银行信息方法
Account_BankManager.ModifyAccount_Bank(id, bankName, amount, res); this.gdvAccounts_Bank.EditIndex = -1; this.bind();
} //取消修改事件
protected void gdvAccounts_Bank_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.gdvAccounts_Bank.EditIndex = -1;
this.bind();
} //删除事件
protected void gdvAccounts_Bank_del(object sender, GridViewDeleteEventArgs e)
{
//获得要删除的编号
int id = Convert.ToInt32(this.gdvAccounts_Bank.DataKeys[e.RowIndex].Value.ToString());
//调用删除方法
Account_BankManager.DeleteAccount_BankByID(id);
this.bind();
} //字段排序事件
protected void gdvAccounts_Bank_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
this.bind(); }
#endregion
不知行不行