介绍下,Form1中有个gridview,当我点击新增,添加一行空白行,这里在gridview列中添加了buttonEdit按钮。
Form1代码:
//物料编码
private void rpbWLBM_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { try { ZP010501Form zp5 = new ZP010501Form(); zp5.ShowDialog(); if (zp5.DialogResult == DialogResult.OK) { getWLInfo(); } } catch (Exception ex) { Logger.Error(ex.Message + "\n" + ex.StackTrace); MsgBox.Error(ex.Message); } }
接下来我们点击按钮弹出窗口~Form2,Form2里也有gridview,里面的数据时从数据库读取出来的。
点击保存时,对Form2的数据进行存入实体类。
Form2代码:
//保存 public static MD_YW_XiangMuMingXi xmmx = null; private void sbtnSave_Click(object sender, EventArgs e) { //选择行 int rowCount = this.gridView1.SelectedRowsCount; if (rowCount > 0) { if (MsgBox.ConfirmYesNo("是否保存?") == DialogResult.Yes) { DataRow myDataRow = gridView1.GetDataRow(gridView1.FocusedRowHandle); string wlms = myDataRow["WL_Desc"].ToString(); string wlmc = wlms.Substring(0, wlms.IndexOf(',')); //物料名称 string ggxh = wlms.Substring(wlms.IndexOf(',') + 1, wlms.Length - 1 - wlms.IndexOf(',')); //规格型号 string ghid = myDataRow["GH_ID"].ToString(); //固化id string wlbm = myDataRow["WL_Code"].ToString(); //物料编码 xmmx = new MD_YW_XiangMuMingXi(); xmmx.Wlmc = wlmc; xmmx.Ggxh = ggxh; xmmx.Ghid = ghid; xmmx.Wlbm = wlbm; } this.DialogResult = DialogResult.OK; } else { MsgBox.Warning("请选择一条物料信息!"); } }
然后是值被带过来:
Form1中添加如下代码:
private void getWLInfo() {//获取gridview的行数 int rownumber = gridView1.RowCount; //string a= ZP010501Form.xmmx.Wlmc; gridView1.SetFocusedRowCellValue("WLBM", ZP010501Form.xmmx.Wlbm); gridView1.SetFocusedRowCellValue("WLMC", ZP010501Form.xmmx.Wlmc); gridView1.SetFocusedRowCellValue("GGXH", ZP010501Form.xmmx.Ggxh); gridView1.SetFocusedRowCellValue("GHID", ZP010501Form.xmmx.Ghid); }