C#中两个表单form, form1相互传值form2,用到字典

表单1,  Form1

private void toolStripButton2_Click(object sender, EventArgs e) //修改
{
    if (dataGridView1.Rows.Count == 0) return;
    int index;
    try
    {
        index = dataGridView1.CurrentRow.Index;
    }
    catch { index = 0; }
    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
    Form2 frm2 = new Form2();
    frm2.transtr = new Dictionary<string, string>();//传递给修改窗体的对应数据字典
    try
    {
        //frm2.t1. = dataGridView1.Rows[index].Cells["Sort"].Value.ToString();
        frm2.transtr["t1"] = dataGridView1.Rows[index].Cells["产品编号"].Value.ToString();
        frm2.transtr["t2"] = dataGridView1.Rows[index].Cells["产品特性"].Value.ToString();
        frm2.transtr["t3"] = dataGridView1.Rows[index].Cells["产品类别"].Value.ToString();
        frm2.transtr["t4"] = dataGridView1.Rows[index].Cells["类别名称"].Value.ToString();
        frm2.transtr["t5"] = dataGridView1.Rows[index].Cells["客户编号"].Value.ToString();
        frm2.transtr["t6"] = dataGridView1.Rows[index].Cells["客户简称"].Value.ToString();
        frm2.transtr["t7"] = dataGridView1.Rows[index].Cells["客户型别"].Value.ToString();
        frm2.transtr["t8"] = dataGridView1.Rows[index].Cells["厂内型别"].Value.ToString();
        frm2.transtr["t9"] = dataGridView1.Rows[index].Cells["球号"].Value.ToString();
        frm2.transtr["t10"] = dataGridView1.Rows[index].Cells["度数"].Value.ToString();
        frm2.transtr["t11"] = dataGridView1.Rows[index].Cells["建立日期"].Value.ToString();
    }
    catch { return; };
    frm2.flag = "xg";
    frm2.ShowDialog();
    try  //修改数据,后又取消
    {
        dataGridView1.Rows[index].Cells["产品编号"].Value = frm2.transtr["t1"];
        dataGridView1.Rows[index].Cells["产品特性"].Value = frm2.transtr["t2"];
        dataGridView1.Rows[index].Cells["产品类别"].Value = frm2.transtr["t3"];
        dataGridView1.Rows[index].Cells["类别名称"].Value = frm2.transtr["t4"];
        dataGridView1.Rows[index].Cells["客户编号"].Value = frm2.transtr["t5"];
        dataGridView1.Rows[index].Cells["客户简称"].Value = frm2.transtr["t6"];
        dataGridView1.Rows[index].Cells["客户型别"].Value = frm2.transtr["t7"];
        dataGridView1.Rows[index].Cells["厂内型别"].Value = frm2.transtr["t8"];
        dataGridView1.Rows[index].Cells["球号"].Value = frm2.transtr["t9"];
        dataGridView1.Rows[index].Cells["度数"].Value = frm2.transtr["t10"];
        dataGridView1.Rows[index].Cells["建立日期"].Value = frm2.transtr["建立日期"];
        try
        {
            dataGridView1.CurrentCell = dataGridView1.Rows[index + 1].Cells[0];
        }
        catch
        {
            dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
        }
    }
    catch { return; }
}

 

表单2, Form2

private Dictionary<string, string> _transtr; //传递值字典
public Dictionary<string, string> transtr
{
    set { _transtr = value; }
    get { return _transtr; }
}
private void button2_Click(object sender, EventArgs e) //确定
{
    if (flag == "xg") //修改数据
    {
        transtr = new Dictionary<string, string>();
        foreach (Control ctrl in this.Controls)
        {
            transtr.Add(ctrl.Name, ctrl.Text);
        }
        transtr.Add("建立日期", dateTimePicker1.Value.ToString("yyyy-MM-dd"));
        MessageBox.Show("修改数据成功", "Error");
        return;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值