在 Winforms 应用程序中将两个表单中的数据保存到同一个表中 [英] Save data from two forms into same table in Winforms application

查看:23
本文介绍了在 Winforms 应用程序中将两个表单中的数据保存到同一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Winforms,其中表单 1 用于输入前七个字段的数据,其他表单用于输入后三个字段,其中我将表存储在 SQL Server 中,但问题是每当我尝试保存时第二种形式的数据存储在第一行本身而不是第一种形式正在更新的行中.任何人都可以帮助如何链接这两个表单并将它们保存在同一行中?

I have two Winforms where form 1 is used to enter data from first seven fields and other forms is used to enter for last three fields, where I have the table stored in SQL Server, but the problem is whenever I tried to save data in second forms it is getting stored in first row itself instead of the row where the first form is updating. Can anyone please help how to link these two forms and get them saved in the same row?

这是第二种形式保存数据的代码,附截图:

This is the code for saving the data in the second form and screenshots are attached:

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using System.Data.SqlClient;  

namespace Windows  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
          
        private void btnInsert_Click(object sender, EventArgs e)  
        {  
            SqlConnection con = new SqlConnection(cs);  
            SqlCommand cmd;  
            con.Open();  
            string s="insert into Student values(@p1,@p2,@p3)";  

            cmd = new SqlCommand(s,con);  
            cmd.Parameters.AddWithValue("@p1",rejectReason1ComboBox.Text);  
            cmd.Parameters.AddWithValue("@p2",rejectReason2ComboBox.Text);  
            cmd.Parameters.AddWithValue("@p3",rejectReason3ComboBox.Text);  
            cmd.CommandType = CommandType.Text;  

            int i = cmd.ExecuteNonQuery();  
            con.Close();  

            MessageBox.Show(i+ " Row(s) Inserted ");  
        }  
    }  
}  

推荐答案

您可以像这样在 ChildForm 中添加公共属性:

You can add public properties in your ChildForm like this:

public string Reason1 {get; set;}
public string Reason2 {get; set;}

private void Combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
     Reason1 = (sender as ComboBox).SelectedItem.ToString();
}

private void ButtonOK_Click(object sender, EventArgs e)
{
     (sender as Button).DialogResult = DialogResult.OK;
     // Or you can set this DialogResult property from Button's property window
}

然后在ParentForm中,像这样使用ChildForm

private void ButtonOpenChildForm_Click(object sender, EventArgs e)
        {
            using (ChildForm form = new ChildForm())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    // Receive the Reasons here in any variable
                    string reason1 = form.Reason1;
                    string reason2 = form.Reason2;
                }
            }
        }

这篇关于在 Winforms 应用程序中将两个表单中的数据保存到同一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆