WinForm:数据源DataTable保持与DataGridView 界面数据的修改实时同步

Milk- 2011-07-07 12:12:23
在WinForm应用程序中,采用DataTable向 DataGridView 中绑定数据。
这个 DataGridView 中不光是数据呈现,还有TextBox、ComboBox等,当向 TextBox里输入值、ComboBox选项发生改变,希望把这些值同步更新到原DataTable。
比如第一行第二列是姓名字段,页面加载时DataTable中是空字符串(即无姓名),DataGridView 上以TextBox形式呈现,当用户输入“张三”后,则把这个姓名信息同步到 DataTable中,使得在不关闭窗体前提下,再一次绑定DataTable到DataGridView 后,原来输入的姓名“张三”能出现。
求相关实现的参考。
...全文
978 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruanwei1987 2011-07-07
  • 打赏
  • 举报
回复
这是 telerik 里面的radgridview 实例一部分
ruanwei1987 2011-07-07
  • 打赏
  • 举报
回复

private void UpdateGridInfo(GridViewRowInfo currentRow)
{
if (currentRow == null)
{ return; }
this.radGridView1.CloseEditor();
currentRow.Cells["ProductName"].Value = this.radTextBoxProductName.Text;
currentRow.Cells["Manufacturer"].Value = this.radTextBoxManufacturer.Text;
urrentRow.Cells["Lining"].Value = this.radTextBoxMaterial.Text;
currentRow.Cells["Dimensions"].Value = this.radTextBoxDimension.Text;
if (this.radComboBox1.SelectedIndex != -1)
{
currentRow.Cells["SalesRepresentative"].Value = ((RadComboBoxItem)this.radComboBox1.SelectedItem).Text;
}
GridViewNewRowInfo newRowInfo = currentRow as GridViewNewRowInfo;
if (newRowInfo != null)
{ currentRow.InvalidateRow(); }
else {
((IEditableObject)this.radGridView1.CurrentRow.DataBoundItem).EndEdit();
}
}
ruanwei1987 2011-07-07
  • 打赏
  • 举报
回复
在 rowchange 事件里面做更新

如果 row发生

private void UpdateGridInfo(GridViewRowInfo currentRow) { if (currentRow == null) { return; } this.radGridView1.CloseEditor(); currentRow.Cells["ProductName"].Value = this.radTextBoxProductName.Text; currentRow.Cells["Manufacturer"].Value = this.radTextBoxManufacturer.Text; currentRow.Cells["Lining"].Value = this.radTextBoxMaterial.Text; currentRow.Cells["Dimensions"].Value = this.radTextBoxDimension.Text; if (this.radComboBox1.SelectedIndex != -1) { currentRow.Cells["SalesRepresentative"].Value = ((RadComboBoxItem)this.radComboBox1.SelectedItem).Text; } GridViewNewRowInfo newRowInfo = currentRow as GridViewNewRowInfo; if (newRowInfo != null) { currentRow.InvalidateRow(); } else { ((IEditableObject)this.radGridView1.CurrentRow.DataBoundItem).EndEdit(); } }
Milk- 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jonnysun 的回复:]
解决方案:

1. 表格的数据源来自视图或者Join两张表,假设有a.CustomerCode,b.CustomerName字段,取到数据后能准确显示名称如“张三”

2. 当在GridView中新增记录,在CustomerCode栏输入C01编号,假设带出CustomerName为“李四”,然后更新当前记录的CustomerName字段,在缓存的的数据中也能显示

3. 保存数据后,……
[/Quote]

您好,我描述的所有操作,皆不向数据库存储。 当向DataGridView 内的TextBox输入一个“张”字时,立刻将这个信息同步到原 DataTable。
JonnySun 2011-07-07
  • 打赏
  • 举报
回复
如果用到第三方控件,太简单了。

参考这两篇文章吧,小菜鸟:

开发应用-RepositoryItemlookupEdit显示名称
http://www.csframework.com/archive/1/arc-1-20110327-1242.htm

表格内显示多表关联数据解决方案(Join,View,lookup)
http://www.csframework.com/archive/1/arc-1-20110613-1594.htm
JonnySun 2011-07-07
  • 打赏
  • 举报
回复
解决方案:

1. 表格的数据源来自视图或者Join两张表,假设有a.CustomerCode,b.CustomerName字段,取到数据后能准确显示名称如“张三”

2. 当在GridView中新增记录,在CustomerCode栏输入C01编号,假设带出CustomerName为“李四”,然后更新当前记录的CustomerName字段,在缓存的的数据中也能显示

3. 保存数据后,重新查询,进入步驟1

机器人 2011-07-07
  • 打赏
  • 举报
回复
用BindingSource实现双向绑定就行了。

比如:txtName.DataBindings.Add("Text", 数据源, "列名");

http://blog.csdn.net/fangxinggood/article/details/6329840
  • 打赏
  • 举报
回复
帮顶! 求分。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧