【问题标题】:Display cell value on textbox in c#在c#中的文本框上显示单元格值
【发布时间】:2019-02-24 21:53:13
【问题描述】:

我的问题是,当我单击“价格”列下的单元格时,它只会在文本框中显示数据网格的内容,该单元格的“货币数据类型”除 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();
    }

【问题讨论】:

  • 为什么将代码发布为图片?
  • 这里的新手不知道如何道歉
  • 好的,欢迎来到 Stackoverflow :) 以下是您可能想阅读的内容:stackoverflow.com/help/how-to-ask,这是提问时要记住的最重要事项的清单:codeblog.jonskeet.uk/2012/11/24/…
  • 请不要将您的代码发布为图像,因为这会导致无法复制和粘贴,并且搜索引擎难以索引。提问时最好包括您的代码、预期输出、当前错误以及您可能尝试过的任何步骤。这将使社区更容易为您提供帮助。
  • 注明。我已经编辑了我的问题:) 谢谢

标签: c# datagridview textbox cell display


【解决方案1】:

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

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

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

【讨论】:

  • Cellclick 对我有用,而不是 cellcontentclick,非常感谢!
【解决方案2】:

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

    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();
    }

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多