簡體   English   中英

在Datagridview中選擇行到文本框

[英]Selecting rows in Datagridview to textbox

我希望將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 USERNAME=@USERNAME,PASSWORD=@PASSWORD where Id=@id", 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 Id=@id", 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();
    }
  }
}

感謝您的回應! 我也無法使它正常工作。 我正在遵循此處的指南http://www.c-sharpcorner.com/uploadfile/1e050f/insert-update-and-delete-record-in-datagridview-c-sharp/

我最終下載並打開了他的項目。 看起來像我做了dataGridView1_RowHeaderMouseClick()之后,我需要進入datagridview選項並選擇RowHeaderMouseClick選項。

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

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

之后,在網格中選擇一行將自動使用該行“ ColumnName”列中的值填充TextBox TextBox編輯值也將自動更新網格。 這就是綁定的全部要點,即綁定一端發生的事情也會影響另一端。 在這種情況下,影響一個控件也會影響數據源,而影響數據源也會影響另一控件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM