两个窗体之间传递对象

   public ArchiExt ArchiIdGetInfo(string ArchiId)
        {

stringsql="selecta.AddDatetime,a.ArchiId,a.ArchiNumber,a.ArchiName,b.ArchiNameList,c.DeName,a.ArchiBorrowInfo,a.BorrowName,a.ArchiPlace,a.Comment,a.Image from ArchivesInfo a";
sql += " inner join ArchiLB b on a.ArchiList=b.ArchiList";
sql += " inner join Department c on a.DeId=c.DeId where ArchiId= " + ArchiId;
            try
            {
                SqlDataReader objdr = SqlHelp.GetInfo(sql);
                ArchiExt objext = null;
                if (objdr.Read())
                {
                    objext = new ArchiExt()
                    {
                        ArchiId = Convert.ToInt32(objdr["ArchiId"]),
                        ArchiNumber = objdr["ArchiNumber"].ToString(),
                        ArchiName = objdr["ArchiName"].ToString(),
                        cDeName = objdr["DeName"].ToString(),
                        ArchiBorrowInfo = objdr["ArchiBorrowInfo"].ToString(),
                        BorrowName = objdr["BorrowName"].ToString(),
                        ArchiPlace = objdr["ArchiPlace"].ToString(),
                        Comment = objdr["Comment"].ToString(),
                        AddDatetime = Convert.ToDateTime(objdr["AddDatetime"]),
                        bArchiName = objdr["ArchiNameList"].ToString(),

                    };
                }

                return objext;
            }
            catch (Exception ex)
            {
                
                throw new Exception("数据查询错误"+ex.Message);
            }

比如:我在窗体A中有个查询的结果集 根据结果集中的序号来点击修改,就显示数据库对应的内容;

首先呢,我们要写个方法返回一个对象,public 对象 getstring 序号

我们要显示的对象已经出来了,接下来要把对象的数据现在是另一个窗体中需要在另一个窗体中写一个带参数的构造方法!完了对象传递过去,赋值给控件就好了

public partial class FrmModify : Form
    {
        public FrmModify()
        {
            InitializeComponent();
        }
        public FrmModify(ArchiExt objext)
        {
            InitializeComponent();
            this.textBox1.Text = objext.ArchiNumber;
            this.textBox2.Text = objext.ArchiName;
            this.textBox3.Text = objext.BorrowName;
            this.textBox4.Text = objext.ArchiPlace;
            this.textBox5.Text = objext.Comment;
            this.textBox6.Text = objext.cDeName;
            this.textBox7.Text = objext.bArchiName;
            this.dateTimePicker1.Text = objext.AddDatetime.ToString();
            this.textBox8.Text = objext.ArchiBorrowInfo;
           // this.pictureBox1.Image=objext.Image
        }
原文地址:https://www.cnblogs.com/xiaowie/p/8664039.html