DataGridView窗体传值方式
发布日期:2022-02-26 00:17:45 浏览次数:9 分类:技术文章

本文共 3533 字,大约阅读时间需要 11 分钟。

 

关于窗体之间有传值,方法有很多,大家找到合适自己的方法,习惯使用就行了,当然,代码越简洁越容易理解越好。以下提供三种窗体传值的方法供大家参考:

 

比如:单击某行数据,点修改时,把这行数据传递到另外修改的窗体中,并且显示相应的数据:

实现方式有三种:

第一种:通过ID查询商品信息并给窗体赋值(用DataReader)

在修改商品窗体代码中设置一下全局变量,商品编号,在点击修改窗体时,获取当前选中的这行数据里的商品编号,再在修改商品这个窗体加载的方法里,根据ID查询出商品信息。

1)、在frmEditCommodtiy窗体代码中:

    public DataGridViewRow dgvr;//定义了一个数据行对象

2)、在弹出某个窗体中,设置获取当前选中的商品ID,并赋值给frmEditCommodtiy的商品ID中

        //弹出修改窗体

        private void tsbtnUpdate_Click(object sender, EventArgs e)
        {
            frmEditCommodtiy fec = new frmEditCommodtiy();
            //获取当前选中的行,再获取其中的商品编号:
            //方法1:获取当前选中的行,再取得其中的列(0可以改成列名)
            fec.CommodityId = Convert.ToInt32(dgvCommodity.CurrentRow.Cells[0].Value);
            //方法2:DataGridView[列,行],获取当前选中的行(0可以改成列名)
            // dgvCommodity[0, dgvStudent.CurrentCell.RowIndex].Value.ToString();
            //方法3:获取当前选中的列集合其中的某列:DataGridView.SelectedCells[0].Value;(0可以改成列名)
            //方法4:获取当前选中的行集合其中的某单元格:dgvCommodity.SelectedRows[0].Cells[0].Value;
            fec.ShowDialog();
        }

4、在新窗体加载中,窗体加载时先加载下拉列表(商品类别):

     //加载下拉列表

        public void FillSort()
        {
            string sql = "select * from dbo.CommoditySort";
            ds = new DataSet();
            adapter = new SqlDataAdapter(sql, _db.Conn);
            adapter.Fill(ds, "sort");
            cmbSort.DataSource = ds.Tables["sort"];
            cmbSort.DisplayMember = "SortName";
            cmbSort.ValueMember = "SortID";
        }

5、根据ID查询出商品信息,并赋值在窗体的控件中

     //根据ID查询商品信息,并显示在页面上,用DataReader

        public void GetCommodity()
        {
            string sql = string.Format("select * from dbo.Commodity where CommodityId={0}",CommodityId);
            SqlDataReader reader = _db.DbExcuterReader(sql);
            if (reader != null)
            {
                if (reader.Read())
                {
                    txtComName.Text = reader["CommodityName"].ToString();
                    cmbSort.SelectedValue = Convert.ToInt32(reader["SortID"]);
                    nudCommodityPrice.Value = Convert.ToDecimal (reader["CommodityPrice"]);
                    int discount = Convert.ToInt32(reader["IsDiscount"]);
                    if (discount == 1)
                    {
                        cbDiscount.Checked = true;
                    }
                    else
                    {
                        cbDiscount.Checked = false;
                    }
                    nudReducedPrice.Value = Convert.ToDecimal(reader["ReducedPrice"]);
                }
            }
        }

6、修改功能自己完成,注意要先验证,修改时用ExcuteNoQuery方法。

第二种:通过ID查询商品信息并给窗体赋值(用DataSet)

前四步和上一种方式相同,在第五步时改为:

    //根据ID查询商品信息,并显示在页面上,用DataSet

        public void GetCommodity2()
        {
            string sql = string.Format("select * from dbo.Commodity where CommodityId={0}", CommodityId);
            ds = new DataSet();
            adapter = new SqlDataAdapter(sql, _db.Conn);
            adapter.Fill(ds, "Com");
            txtComName.Text = ds.Tables["Com"].Rows[0][1].ToString();
            cmbSort.SelectedValue = ds.Tables["Com"].Rows[0][2].ToString();
            nudCommodityPrice.Value =Convert.ToDecimal(ds.Tables["Com"].Rows[0][3]);      
            int isDiscount = Convert.ToInt32(ds.Tables["Com"].Rows[0][4]);
            if (isDiscount == 1)
            {
                cbDiscount.Checked = true;
            }
            else
            {
                cbDiscount.Checked = false;
            }
            nudReducedPrice.Value = Convert.ToDecimal(ds.Tables["Com"].Rows[0][5]);
        }

第三种:通过选中某行时,获取当前整行数据,传递并给窗体赋值(推荐)

1、         在frmEditCommodtiy窗体代码中:

a)        public DataGridViewRow dgvr;//定义了一个数据行对象

2、         在弹出某个窗体中,设置获取当前选中这行数据,并赋值给frmEditCommodtiy的商品DataGridRViewRow中

       //弹出修改窗体

        private void tsbtnUpdate_Click(object sender, EventArgs e)
        {
            frmEditCommodtiy fec = new frmEditCommodtiy();
            DataGridViewRow drv = dgvCommodity.CurrentRow;//获取当前选中的一行数据
            fec.dgvr = drv;//传递过去对方窗体的DataGridViewRow
            fec.ShowDialog();
        }

3、给窗体的控件赋值

     //把传递过来的数据,赋值到窗体的控件中

        public void GetCommodity3()
        {
            txtComName.Text = dgvr.Cells[1].Value.ToString();
            cmbSort.Text = dgvr.Cells[2].Value.ToString();
            nudCommodityPrice.Value = Convert.ToDecimal(dgvr.Cells[3].Value);
            int isDiscount = Convert.ToInt32(dgvr.Cells[4].Value);//是否特价
            if (isDiscount == 1)
            {
                cbDiscount.Checked = true;
            }
            else
            {
                cbDiscount.Checked = false;
            }
            nudReducedPrice.Value = Convert.ToDecimal(dgvr.Cells[5].Value);
        }

其他的修改功能自己完成。

个人建议用最后一种方式,传递整行数据过来。方法是多种多样的,希望大家找到自己合适的方法,并且多去练!加油!

转载地址:https://blog.csdn.net/tml237615629/article/details/6992788 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java 泛型
下一篇:os-rammanage

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月17日 03时17分01秒