繁体   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