2016-12-24 43 views
0

我正在將datagridview中單元格的內容移動到文本框中。但是,我似乎無法讓它工作。將用戶添加到數據庫按鈕我已經測試並且工作正常,它只是從數據網格視圖中選擇的。在Datagridview中選擇文本框中的行

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 TEST 
{ 
    public partial class AddUsers : Form 
    { 
     SqlConnection con = new SqlConnection(@"Data Source(LocalDB)\MSSQLLocalDB;AttachDbFilename=mydb;Integrated Security=True;Connect Timeout=30"); 
     SqlCommand cmd; 
     SqlDataAdapter adapt; 
     int ID = 0; 
     public AddUsers() 
     { 
      InitializeComponent(); 
      DisplayData(); 
     } 
     private void selectRow(object sender, DataGridViewCellMouseEventArgs e) 
     { 
      ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()); 
      textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); 
      textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); 
     } 
     //Update Record 
     private void button2_Click(object sender, EventArgs e) 
     { 
      if (textBox1.Text != "" && textBox2.Text != "") 
      { 
       cmd = new SqlCommand("update tblLOGIN set [email protected],[email protected] where [email protected]", con); 
       con.Open(); 
       cmd.Parameters.AddWithValue("@Id", ID); 
       cmd.Parameters.AddWithValue("@USERNAME", textBox1.Text); 
       cmd.Parameters.AddWithValue("@PASSWORD", textBox2.Text); 
       cmd.ExecuteNonQuery(); 
       MessageBox.Show("Record Updated Successfully"); 
       con.Close(); 
       DisplayData(); 
       ClearData(); 
      } 
      else 
      { 
       MessageBox.Show("Please Select Record to Update"); 
      } 
     } 
     //New User 
     private void button1_Click(object sender, EventArgs e) 
     { 
      if (textBox1.Text != "" && textBox2.Text != "") 
      { 
       cmd = new SqlCommand("insert into tblLOGIN(USERNAME,PASSWORD) values(@USERNAME,@PASSWORD)", con); 
       con.Open(); 
      cmd.Parameters.AddWithValue("@USERNAME", textBox1.Text); 
      cmd.Parameters.AddWithValue("@PASSWORD", textBox2.Text); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      MessageBox.Show("User created successfully"); 
      DisplayData(); 
      ClearData(); 
     } 
     else 
     { 
      MessageBox.Show("Please enter username and password"); 
     } 
    } 
    private void DisplayData() 
    { 
     con.Open(); 
     DataTable dt = new DataTable(); 
     adapt = new SqlDataAdapter("select * from tblLOGIN", con); 
     adapt.Fill(dt); 
     dataGridView1.DataSource = dt; 
     con.Close(); 
    } 
      private void ClearData() 
    { 
     textBox1.Text = ""; 
     textBox2.Text = ""; 
     ID = 0; 
    } 
    //Delete user 
    private void button3_Click(object sender, EventArgs e) 
    { 
     if (ID != 0) 
     { 
      cmd = new SqlCommand("delete tblLOGIN where [email protected]", con); 
      con.Open(); 
      cmd.Parameters.AddWithValue("@id", ID); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      MessageBox.Show("Record Deleted Successfully!"); 
      DisplayData(); 
      ClearData(); 
     } 
     else 
     { 
      MessageBox.Show("Please Select Record to Delete"); 
     } 
    } 

     private void AddUsers_Load(object sender, EventArgs e) 
     { 
      this.tblLOGINTableAdapter.Fill(this.usersDataSet.tblLOGIN); 
     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
     AuthenicationForm ss = new AuthenicationForm(); 
     this.Close(); 
     ss.Show(); 
    } 
    } 
} 

回答

0

你不需要任何代碼就可以做你想做的事情。只需將您的TextBoxes和/或其他控件綁定到與網格相同的數據源,例如。

myBindingSource.DataSource = myDataTable 
myDataGridView.DataSource = myBindingSource 
myTextBox.DataBindings.Add("Text", myBindingSource, "ColumnName") 

在此之後,在網格中選擇一行將自動從該行的「的ColumnName」列中的值填充TextBox。編輯TextBox中的值也會自動更新網格。這就是結合的要點,即結合的一端發生什麼也會影響另一端。在這種情況下,影響一個控件也會影響數據源並影響數據源影響另一個控件。