GridControl或DataGridView修改数据后如何立即更新显示

xiaoforward 2015-10-19 07:45:07
大家好:
问题如题所示,双击Form1表中某一列的值,弹出Form2修改对话框,修改数据后点击保存,将数据更新到数据库后GridControl或DataGridView中数据也立即更新。
我按照如下步骤进行:
1. 双击表中的列,弹出修改对话框,对话框中的控件(textbox、combobox)显示表中的列值。
2. 根据需要修改对应的值,点击保存按钮,将数据更新到sql server数据库中(update)。
3. 点击保存后可以sql server中查询出具体修改后的值,证明修改已经成功。
但是,通过怎样的代码可以实现在点击修改窗体Form2“保存”按钮后Form1窗体中的表立即更新(刷新到最新)?
图片:

代码如下:
1. Form1窗体启动时加载及显示数据:

SystemManagerProfileListDAL systemManagerProfileListDAL = new SystemManagerProfileListDAL();
DataTable table = systemManagerProfileListDAL.ListAllData(); //从数据库中取出所有数据
//为GridControl绑定数据
grcShapeProfile.DataSource = table; //GridControl数据源绑定Table
CommonClass.CommonHelper.MatchColumn(table, grvMain);

2. 双击列弹出修改对话框:

private void grvMain_DoubleClick(object sender, EventArgs e)
{
FormShapeProfileModifyAdd formShapeProfileModifyAdd = new FormShapeProfileModifyAdd();
DataRow selectRow = grvMain.GetDataRow(grvMain.FocusedRowHandle); //获取GridCotnrol MainView选中行的数据
ID = (Guid)selectRow[0]; //GUID
ItemNo = Convert.ToInt32(selectRow[1]); //序号
SysMaProList = (string)selectRow[2]; //型材类别(国标/欧标)
SysMaProListName = (string)selectRow[3];//型材名称
SysMaProListProfile = (string)selectRow[4]; // 型材规格
SysMaProListUnitWeight = Convert.ToDouble(selectRow[5]); //单位重量Kg/m
SysMaProListUnitArea = Convert.ToDouble(selectRow[6]); //单位表面积m²/m
SysMaProListRefStandard = (string)selectRow[7]; //参考标准
SysMaProListRemark = (string)CommonClass.CommonHelper.FromDbValue(selectRow[8]); //备注
formShapeProfileModifyAdd.Text = "修改"; //窗体标题赋值
formShapeProfileModifyAdd.Show(); //显示窗体

}

3. 修改对话框保存按钮代码:
[code=csharp]
private void btnSave_Click(object sender, EventArgs e)
{
if (this.Text == "修改")
{
SystemManagerProfileList systemManagerProfileList = new SystemManagerProfileList();
SystemManagerProfileListDAL systemManagerProfileListDAL = new SystemManagerProfileListDAL();
systemManagerProfileList.ID = FormShapeProfile.ID;
systemManagerProfileList.ItemNo = Convert.ToInt32(txtShapeItem.Text);
systemManagerProfileList.SysMaProList = cmbShapeType.Text;
systemManagerProfileList.SysMaProListName = cmbShapeName.Text;
systemManagerProfileList.SysMaProListProfile = txtShapeProfile.Text;
systemManagerProfileList.SysMaProListUnitWeight = Convert.ToDouble(txtWeightPerUnit.Text);
systemManagerProfileList.SysMaProListUnitArea = Convert.ToDouble(txtAreaPerUnit.Text);
systemManagerProfileList.SysMaProListRefStandard = cmbRefStandard.Text;
systemManagerProfileList.SysMaProListRemark = txtShapeRemark.Text;

systemManagerProfileListDAL.Update(systemManagerProfileList); //更新数据库中的数据

//下面代码不起作用,该如何写?
DataTable table = systemManagerProfileListDAL.ListAllData(); //从数据库中取出所有数据
//为GridControl绑定数据
FormShapeProfile formShapeProfile = new FormShapeProfile();
GridControl grcShapeProfile = new GridControl();
grcShapeProfile = (GridControl)formShapeProfile.Controls["grcShapeProfile"];
grcShapeProfile.DataSource = table; //GridControl数据源绑定Table
CommonClass.CommonHelper.MatchColumn(table, (GridView)grcShapeProfile.MainView);

}
[/code
如何进行修改
...全文
4238 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
alicellly 2016-02-18
  • 打赏
  • 举报
回复
请问一下更新数据是怎么实现的 ,可以说的详细一点吗
ajianchina 2015-10-20
  • 打赏
  • 举报
回复
你的代码我还未仔细查看,不过如果你的保存数据的操作已在Form2中完成的话,最简单的方法,也就是Form1的数据源做成他的全局变量,每次修改完成后其实GridView的数据源都不用重新查询,只要在数据源变量中修改一下,然后重新绑定即可。
xiaoforward 2015-10-20
  • 打赏
  • 举报
回复
已经解决,在重新绑定数据后执行查询按钮的点击方法 btnSearch.PerformClick()
xiaoforward 2015-10-20
  • 打赏
  • 举报
回复
引用 4 楼 ajianchina 的回复:
在你的Form1中写一个修改数据的public方法:

public void 修改DataGridView行数据(dataClass data)
{
	//根据传入进来的data中的id在DataGridView的数据源中进行修改,完成后重新绑定。
	//如果你原先DataGridView的被修改后处于选中状态,希望更新后还保持原来的状态,可以遍历一下所有行,按条件重新设定DataGridView的FirstDisplayedScrollingRowIndex,这样就处于被选中状态了。
}
接着在你的Form2中,在修改数据的btnSave_Click事件里面,在修改完成后执行一下这样一个方法:

if (this.Owner is Form1) ((Form1)this.Owner).修改DataGridView行数据(data);
这样就行了,当然你通过委托来写也是可以的。
这个问题解决了,但是有个新的问题: 我在窗体中点击查询按钮,我只想修改国标的数据,修改完成后点击保存,因重新加载数据,则查询状态取消,我现在想修改保存数据后,查询状态还存在,该如何实现? 谢谢!

 public void UpdateShowData(SystemManagerProfileList systemManagerProfileList)
        {
            SystemManagerProfileListDAL systemManagerProfileListDAL = new SystemManagerProfileListDAL();
            systemManagerProfileListDAL.Update(systemManagerProfileList); //更新数据库中的数据
            DataTable table = systemManagerProfileListDAL.ListAllData(); //从数据库中取出所有数据
            //为GridControl绑定数据
            grcShapeProfile.DataSource = table; //GridControl数据源绑定Table
            CommonClass.CommonHelper.MatchColumn(table, grvMain);
        }
xiaoforward 2015-10-20
  • 打赏
  • 举报
回复
引用 7 楼 ajianchina 的回复:
这是最简单可行的步骤,保证你可以用,不行找我,负责到底。
搞定,不过在Form1中显示Form2窗体时要指定owner属性,即Form2属于Form1 Form2 f2=new Form2() f2.Owner=this f2.ShowDialog() 或者f2.ShowDialog(this) 分全部你的了!
u010750886 2015-10-20
  • 打赏
  • 举报
回复
Form2 form2 = new Form2(Data); form2.ShowDialog(); if(form2.DialogResult = true) { //访问form2.systemManagerProfileList属性更新form1 } //在form2里 public systemManagerProfileList; systemManagerProfileList = Data //....对systemManagerProfileList进行操作,更新数据库,关闭窗口
xiaoforward 2015-10-20
  • 打赏
  • 举报
回复
引用 7 楼 ajianchina 的回复:
这是最简单可行的步骤,保证你可以用,不行找我,负责到底。
ajianchina 2015-10-20
  • 打赏
  • 举报
回复
这是最简单可行的步骤,保证你可以用,不行找我,负责到底。
xiaoforward 2015-10-20
  • 打赏
  • 举报
回复
引用 4 楼 ajianchina 的回复:
在你的Form1中写一个修改数据的public方法:

public void 修改DataGridView行数据(dataClass data)
{
	//根据传入进来的data中的id在DataGridView的数据源中进行修改,完成后重新绑定。
	//如果你原先DataGridView的被修改后处于选中状态,希望更新后还保持原来的状态,可以遍历一下所有行,按条件重新设定DataGridView的FirstDisplayedScrollingRowIndex,这样就处于被选中状态了。
}
接着在你的Form2中,在修改数据的btnSave_Click事件里面,在修改完成后执行一下这样一个方法:

if (this.Owner is Form1) ((Form1)this.Owner).修改DataGridView行数据(data);
这样就行了,当然你通过委托来写也是可以的。
我看网上说使用showdialog也可以,这个是什么步骤?
xiaoforward 2015-10-20
  • 打赏
  • 举报
回复
引用 3 楼 liming0605 的回复:
{ if (this.Text == "修改") { SystemManagerProfileList systemManagerProfileList = new SystemManagerProfileList(); SystemManagerProfileListDAL systemManagerProfileListDAL = new SystemManagerProfileListDAL(); systemManagerProfileList.ID = FormShapeProfile.ID; systemManagerProfileList.ItemNo = Convert.ToInt32(txtShapeItem.Text); systemManagerProfileList.SysMaProList = cmbShapeType.Text; systemManagerProfileList.SysMaProListName = cmbShapeName.Text; systemManagerProfileList.SysMaProListProfile = txtShapeProfile.Text; systemManagerProfileList.SysMaProListUnitWeight = Convert.ToDouble(txtWeightPerUnit.Text); systemManagerProfileList.SysMaProListUnitArea = Convert.ToDouble(txtAreaPerUnit.Text); systemManagerProfileList.SysMaProListRefStandard = cmbRefStandard.Text; systemManagerProfileList.SysMaProListRemark = txtShapeRemark.Text; systemManagerProfileListDAL.Update(systemManagerProfileList); //更新数据库中的数据 //下面代码不起作用,该如何写? DataTable table = systemManagerProfileListDAL.ListAllData(); //从数据库中取出所有数据 //为GridControl绑定数据 FormShapeProfile formShapeProfile = new FormShapeProfile(); GridControl grcShapeProfile = new GridControl(); grcShapeProfile = (GridControl)formShapeProfile.Controls["grcShapeProfile"]; grcShapeProfile.DataSource = table; //GridControl数据源绑定Table CommonClass.CommonHelper.MatchColumn(table, (GridView)grcShapeProfile.MainView); }
神马意思?
ajianchina 2015-10-20
  • 打赏
  • 举报
回复
在你的Form1中写一个修改数据的public方法:

public void 修改DataGridView行数据(dataClass data)
{
	//根据传入进来的data中的id在DataGridView的数据源中进行修改,完成后重新绑定。
	//如果你原先DataGridView的被修改后处于选中状态,希望更新后还保持原来的状态,可以遍历一下所有行,按条件重新设定DataGridView的FirstDisplayedScrollingRowIndex,这样就处于被选中状态了。
}
接着在你的Form2中,在修改数据的btnSave_Click事件里面,在修改完成后执行一下这样一个方法:

if (this.Owner is Form1) ((Form1)this.Owner).修改DataGridView行数据(data);
这样就行了,当然你通过委托来写也是可以的。
liming0605 2015-10-20
  • 打赏
  • 举报
回复
{ if (this.Text == "修改") { SystemManagerProfileList systemManagerProfileList = new SystemManagerProfileList(); SystemManagerProfileListDAL systemManagerProfileListDAL = new SystemManagerProfileListDAL(); systemManagerProfileList.ID = FormShapeProfile.ID; systemManagerProfileList.ItemNo = Convert.ToInt32(txtShapeItem.Text); systemManagerProfileList.SysMaProList = cmbShapeType.Text; systemManagerProfileList.SysMaProListName = cmbShapeName.Text; systemManagerProfileList.SysMaProListProfile = txtShapeProfile.Text; systemManagerProfileList.SysMaProListUnitWeight = Convert.ToDouble(txtWeightPerUnit.Text); systemManagerProfileList.SysMaProListUnitArea = Convert.ToDouble(txtAreaPerUnit.Text); systemManagerProfileList.SysMaProListRefStandard = cmbRefStandard.Text; systemManagerProfileList.SysMaProListRemark = txtShapeRemark.Text; systemManagerProfileListDAL.Update(systemManagerProfileList); //更新数据库中的数据 //下面代码不起作用,该如何写? DataTable table = systemManagerProfileListDAL.ListAllData(); //从数据库中取出所有数据 //为GridControl绑定数据 FormShapeProfile formShapeProfile = new FormShapeProfile(); GridControl grcShapeProfile = new GridControl(); grcShapeProfile = (GridControl)formShapeProfile.Controls["grcShapeProfile"]; grcShapeProfile.DataSource = table; //GridControl数据源绑定Table CommonClass.CommonHelper.MatchColumn(table, (GridView)grcShapeProfile.MainView); }
xiaoforward 2015-10-20
  • 打赏
  • 举报
回复
哪位帮忙解决下!谢谢
xiaoforward 2015-10-19
  • 打赏
  • 举报
回复
3. 修改对话框保存按钮代码:

            private void btnSave_Click(object sender, EventArgs e)
         {
             if (this.Text == "修改")
             {
                 SystemManagerProfileList systemManagerProfileList = new SystemManagerProfileList();
                 SystemManagerProfileListDAL systemManagerProfileListDAL = new SystemManagerProfileListDAL();
                 systemManagerProfileList.ID = FormShapeProfile.ID;
                 systemManagerProfileList.ItemNo = Convert.ToInt32(txtShapeItem.Text);
                 systemManagerProfileList.SysMaProList = cmbShapeType.Text;
                 systemManagerProfileList.SysMaProListName = cmbShapeName.Text;
                 systemManagerProfileList.SysMaProListProfile = txtShapeProfile.Text;
                 systemManagerProfileList.SysMaProListUnitWeight = Convert.ToDouble(txtWeightPerUnit.Text);
                 systemManagerProfileList.SysMaProListUnitArea = Convert.ToDouble(txtAreaPerUnit.Text);
                 systemManagerProfileList.SysMaProListRefStandard = cmbRefStandard.Text;
                 systemManagerProfileList.SysMaProListRemark = txtShapeRemark.Text;

                 systemManagerProfileListDAL.Update(systemManagerProfileList); //更新数据库中的数据
                
                 //下面代码不起作用,该如何写?
                DataTable table = systemManagerProfileListDAL.ListAllData(); //从数据库中取出所有数据
                //为GridControl绑定数据
                FormShapeProfile formShapeProfile = new FormShapeProfile();
                 GridControl grcShapeProfile = new GridControl();
                 grcShapeProfile = (GridControl)formShapeProfile.Controls["grcShapeProfile"];
                 grcShapeProfile.DataSource = table; //GridControl数据源绑定Table
                 CommonClass.CommonHelper.MatchColumn(table, (GridView)grcShapeProfile.MainView);
                  
             }
           

110,565

社区成员

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

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

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