当前位置: 代码迷 >> C# >> GridControl或DataGridView修改数据后怎么立即更新显示
  详细解决方案

GridControl或DataGridView修改数据后怎么立即更新显示

热度:36   发布时间:2016-05-05 02:35:49.0
GridControl或DataGridView修改数据后如何立即更新显示
大家好:
          问题如题所示,双击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
如何进行修改
------解决思路----------------------
在你的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);


这样就行了,当然你通过委托来写也是可以的。
------解决思路----------------------
Form2 form2 = new Form2(Data);
form2.ShowDialog();
if(form2.DialogResult = true)
{
  //访问form2.systemManagerProfileList属性更新form1
}
//在form2里
public  systemManagerProfileList;
systemManagerProfileList = Data
//....对systemManagerProfileList进行操作,更新数据库,关闭窗口
  相关解决方案