【问题标题】:How to change the cell value in a Data Grid when clicked on the cell单击单元格时如何更改数据网格中的单元格值
【发布时间】:2018-09-26 20:18:52
【问题描述】:

我在 c# 中为基于表单的应用程序中的某些应用程序创建了一个 17 列和 65520 行的数据网格。 当我点击任何数据网格单元格时,它的值应该从 0 变为 1。我已经完成了以下点击事件的定义

private void datagridview1_contentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

其中大多数是针对特定单元格的,但对我来说,当在指定范围内(17 列和 65520 行内)单击时,任何单元格中的值都应该从 0 变为 1。因为我是 C# 编码的新手,请帮我解决这个问题。

【问题讨论】:

  • 您的目标是什么:Winforms、WPF、ASP..?您应该始终正确标记您的问题,以便人们可以在问题页面上看到它! - 不要拨打DataGridViewa GridViewDataGrid,反之亦然!!这是错误的和令人困惑的,因为它们是不同的控件。始终以正确的名称称呼事物! - Contentclick 很挑剔:您实际上需要点击内容,而不是空白!建议使用鼠标单击。 -
  • 抱歉问题不完整。我在 Visual Studio 中使用 c# 创建了 Winform .Created DataGridView 我已经编写了上面缺少的函数定义。 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { for (; ; ) { if (e.ColumnIndex != 0 && e.RowIndex != 1) { if (dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value !=空)dataGridView1.CurrentCell.Value = 1; } } }
  • 该函数将在开始时删除无限循环后工作,如果单元格中已经有值,如您编码的那样。

标签: c#


【解决方案1】:

如果您使用 Windows 窗体,这可以帮助您。 因为您只想更改 CellContentClick 事件。有了这个事件,很容易改变它的价值。此代码将单元格值更改为 1:

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1[e.ColumnIndex, e.RowIndex].Value = 1;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多