在 C# 中的文本框上显示单元格值

Display cell value on textbox in c#

我的问题是,当我单击价格列下的单元格时,它只会在文本框上显示数据网格的内容,该单元格具有金钱数据类型,除 ItemNo 外,其他所有数据类型都是 Varchar(100)。请帮忙,谢谢

     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
            DataGridViewRow row = this.dataGridView1.CurrentRow;
            txtDsc.Text = row.Cells["Description"].Value.ToString();
            txtQty.Text = row.Cells["Qty"].Value.ToString();
            txtUnt.Text = row.Cells["Unit"].Value.ToString();
            txtPrc.Text = row.Cells["Price"].Value.ToString();
            txtRmr.Text = row.Cells["Remarks"].Value.ToString();
    }

我不完全确定为什么它只在您单击该单元格时填充;我重新创建了你的代码,它工作得很好。我确实设法使用以下代码重新创建了它:

    private void myDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int row = myDGV.CurrentCell.RowIndex;
        txtDsc.Text = myDGV.Row[row].Cells["Description"].Value.ToString();
        txtQty.Text = myDGV.Row[row].Cells["Qty"].Value.ToString();
        txtUnt.Text = myDGV.Row[row].Cells["Unit"].Value.ToString();
        txtPrc.Text = myDGV.Row[row].Cells["Price"].Value.ToString();
        txtRmr.Text = myDGV.Row[row].Cells["Remarks"].Value.ToString();
    }

正如其他答案所指出的那样,代码似乎运行良好。 我怀疑事件没有触发。尝试放置一个断点并调查该位。

我假设您需要 CellClick 而不是 CellContentClick。 参考 CellContentClick event doesn't always work

或者,如果您尝试根据数据网格视图中的选择显示文本框值,请使用 DataGridView.SelectionChanged 事件