我的項目中有兩種形式(Form1 & Form2)。將Form1文本框值傳遞給Form2按鈕
在Form1上,我有兩個文本框,如「Login」表單,其中用戶需要輸入用戶名(textboxUsername)&密碼(textboxPassword)。當用戶登錄時,Form2彈出。
現在在Form2中我有一個button1和dataGridView1。當我按下按鈕1我想顯示所選用戶,我在Form1中輸入(textboxUsername)
我想根據他的用戶名獲取該用戶,並保存在SQL Server中。
窗體2 Button1的代碼:
private void button1_Click(object sender, EventArgs e)
{
string constring = @"Data Source=V-K\;Initial Catalog=ATMKlientet;Integrated Security=True";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(" select * from Clients ;", conDataBase);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmdDataBase;
dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
在這種情況下,在這裏我想打電話給Form1中textBoxUsername的一部分,在Form2的是:
(" select * from Clients where Username='" + this.textBoxUsername + "';", conDataBase);
此問題的基本答案是不要。有一個用戶類並通過事件鏈接起來(一個接口也會很好),那麼你最終將得到兩個表單「知道」登錄界面而彼此沒有關係。 –