C# 如何将datagridview上的选定数据以单独的形式发送到文本框?

C# 如何将datagridview上的选定数据以单独的形式发送到文本框?,c#,ms-access,datagridview,C#,Ms Access,Datagridview,我有一个存储用户信息的datagridview,它链接到一个ms access数据库。我希望他们能够选择datagridview中的行,然后打开编辑配置文件表单,其中该行的数据已经插入到文本框中。现在我必须将ID插入第一行,这样它就可以读取我所指的行。这是我到目前为止所拥有的 { ConnectToDataBase(); OleDbCommand command = new OleDbCommand(); command.Connection =

我有一个存储用户信息的datagridview,它链接到一个ms access数据库。我希望他们能够选择datagridview中的行,然后打开编辑配置文件表单,其中该行的数据已经插入到文本框中。现在我必须将ID插入第一行,这样它就可以读取我所指的行。这是我到目前为止所拥有的

{
        ConnectToDataBase();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        while (reader.Read())
        {
            textBox1.Text = 
            textBox2.Text = reader.GetValue(2).ToString();
            textBox3.Text = reader.GetValue(3).ToString();
            textBox4.Text = reader.GetValue(4).ToString();
            textBox5.Text = reader.GetValue(5).ToString();
            comboBox1.Text = reader.GetValue(6).ToString();
            comboBox2.Text = reader.GetValue(7).ToString();
            textBox6.Text = reader.GetValue(8).ToString();
        }
        //string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where  ID='" + Convert.ToInt32(textBox7.Text) + "'";
        string query = "update Profiles set [PROFILE NAME]= @Profile, [LOGIN EMAIL]= @Email, [PASSWORD]= @Pass, [FULL NAME]= @Name, [CARD NUMBER]= @Card, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = @Id";
        command.Parameters.AddWithValue("@Profile", textBox1.Text);
        command.Parameters.AddWithValue("@Email", textBox2.Text);
        command.Parameters.AddWithValue("@Pass", textBox3.Text);
        command.Parameters.AddWithValue("@Name", textBox4.Text);
        command.Parameters.AddWithValue("@Card", textBox5.Text);
        command.Parameters.AddWithValue("@EXPM", comboBox1.Text);
        command.Parameters.AddWithValue("@EXPY", comboBox2.Text);
        command.Parameters.AddWithValue("@CVV", textBox6.Text);
        command.Parameters.AddWithValue("@ID", textBox7.Text);
        command.CommandText = query;
        command.ExecuteNonQuery();
        RefreshDBConnection();
        connection.Close();
        MessageBox.Show("Profile Edited");
        this.Close();

谢谢大家!

首先,你的问题一团糟。其次,你的代码乱七八糟。不管怎样,我会在下面给你解释一下

首先,您的问题标题是如何将datagridview上的选定数据以单独的形式发送到文本框? 我猜您在一个表单中有datagirdview,您希望将所选行的数据发送到另一个表单中

以下是两个案例:

第一种情况是在打开新表单时发送数据。所以用户选择行,然后按下按钮并打开另一个表单,该表单需要来自该行的数据

为此,您将使用forms构造函数

现在,我们已经将构造函数更改为requires值,我们从第一个使用datagridview的表单调用此表单,如下所示:

private void Button1_Click(object sender, EventArgs e)
{
    //This is event of button click

    if(yourDataGridView.SelectedRows.Count < 0)
    {
        MessageBox.Show("You must select one user!");
        return;
    }

    DataGridViewRow row = yourDataGridView.SelectedRows[0]; // We will select first selected row. If there are multiple selected rows we will select just first one

    int uid = Convert.ToInt32(row.Cells["ColumnThatContainsUserId"].Value);
    string uname = row.Cells["ColumnThatContainsUserName"].Value.ToString();

    //I am using `using` since `Form` is disposable class and instead of later doing sf.Dispose() i use `using`. This way we take care of perfomance
    using(SecondForm sf = new SecondForm(uid, uname))
    {
        sf.ShowDialog();
    }
}
现在我们有了一个函数,它将用传递的值更新文本框。现在我们需要从第一个表单访问该函数

public partial class FirstForm : Form
{
    private SecondForm sf; //Declaring it so it is accessible from whole first form class

    private int lastSelectedRow = -1; //Will use it later

    public FirstForm()
    {
        //All your code
    }

    private void dataGridView_CellClick(object sender, EventArgs e)
    {
        //This event fires every time user click cell

        if(sf == null)
        {
            //This means our second form has not been initialized so maybe show message to user and return or open it up

            //First solution
            sf = new SecondForm();
            sf.Show();

            //Second solution
            MessageBox.Show("Second form is not initialized!");
            return;
        }

        //I would here check if(sf.Visible) and if it is not i would sf.Show();

        //We check if same row has been selected. If it does then we do nothing
        if(dataGridView.SelectedRows[0].Index == lastSelectedRow)
            return;

        int uid = Convert.ToInt32(row.Cells["ColumnThatContainsUserId"].Value);
        string uname = row.Cells["ColumnThatContainsUserName"].Value.ToString();

       sf.SwitchUser(uid, uname); //Accessing through global variable

       lastSelectedRow = dataGridView.SelectedRows[0].Index;
    }
}

有什么问题?哪里会出错?只是一个猜测:ID是什么类型的?是整数吗?然后,您必须在更新命令中添加一个int值作为参数@ID。感谢您的回复!很抱歉,这是我的第一个c程序,我只编写了大概两周,这就是为什么一切都这么混乱的原因。无论如何,我已经填充了文本框,但是如果我最终没有保存编辑更改,而是想关闭第二个表单,我会遇到以下错误:System.NullReferenceException:“对象引用未设置为对象的实例。”我做了一些调整,解决了这个问题,谢谢!现在效果很好!
public partial class SecondForm : Form
{
    public SecondForm()
    {
        // All your code
    }

    public void SwitchUser(int userId, string userName)
    {
        textbox1.Text = userId.ToString();
        textbox2.Text = userName;
    }
}
public partial class FirstForm : Form
{
    private SecondForm sf; //Declaring it so it is accessible from whole first form class

    private int lastSelectedRow = -1; //Will use it later

    public FirstForm()
    {
        //All your code
    }

    private void dataGridView_CellClick(object sender, EventArgs e)
    {
        //This event fires every time user click cell

        if(sf == null)
        {
            //This means our second form has not been initialized so maybe show message to user and return or open it up

            //First solution
            sf = new SecondForm();
            sf.Show();

            //Second solution
            MessageBox.Show("Second form is not initialized!");
            return;
        }

        //I would here check if(sf.Visible) and if it is not i would sf.Show();

        //We check if same row has been selected. If it does then we do nothing
        if(dataGridView.SelectedRows[0].Index == lastSelectedRow)
            return;

        int uid = Convert.ToInt32(row.Cells["ColumnThatContainsUserId"].Value);
        string uname = row.Cells["ColumnThatContainsUserName"].Value.ToString();

       sf.SwitchUser(uid, uname); //Accessing through global variable

       lastSelectedRow = dataGridView.SelectedRows[0].Index;
    }
}