选择Datagridview中的行到文本框

时间:2016-12-24 01:56:33

标签: c# visual-studio 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 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();
    }
  }
}

2 个答案:

答案 0 :(得分:1)

感谢您的回复!我也无法做到这一点。我正在按照http://www.c-sharpcorner.com/uploadfile/1e050f/insert-update-and-delete-record-in-datagridview-c-sharp/

的指南进行操作

我最终下载并打开了他的项目。看起来在我创建dataGridView1_RowHeaderMouseClick()之后,我需要进入datagridview选项并选择RowHeaderMouseClick选项。

答案 1 :(得分:0)

您不需要任何代码来执行您想要执行的操作。只需将TextBoxes和/或其他控件绑定到与网格相同的数据源,例如

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

之后,在网格中选择一行将自动使用该行的“ColumnName”列中的值填充TextBox。编辑TextBox中的值也会自动更新网格。这就是整个绑定点,即绑定的一端发生的事情也会影响另一端。在这种情况下,影响一个控件也会影响数据源,影响数据源会影响其他控件。