【问题标题】:Show value of the second column in the first column (as a fallback if first column is empty)在第一列中显示第二列的值(如果第一列为空,则作为后备)
【发布时间】:2019-08-29 07:03:42
【问题描述】:

所以我的 DataGridView 有两列,分别名为“Phone1”和“Phone2”。

问题是当Phone1为空时,可能是Phone2中有一个电话号码。如果 Phone1 为空,但如果 Phone2 不是,那么如何在 C# 中签入,Phone2 中的文本会转到 Phone1?

假设以下输入,Phone1 在第二行和第三行中没有值:

┌───────────┬───────────┐
│ Phone1    │ Phone2    │
├───────────┼───────────┤
│ 1111111   │ 2222222   │
│           │ 3333333   │
│           │ 4444444   │
│ 5555555   │           │
└───────────┴───────────┘

这是预期的输出,Phone2 的值已显示为后备:

┌───────────┬───────────┐
│ Phone1    │ Phone2    │
├───────────┼───────────┤
│ 1111111   │ 2222222   │
│ 3333333   │ 3333333   │
│ 4444444   │ 4444444   │
│ 5555555   │           │
└───────────┴───────────┘

那么,如果第一列为空,我可以在第一列中显示第二列的值作为后备吗?

【问题讨论】:

  • 网络表单?赢得表格? WPF? UWP? XAML?
  • WinForms.. 对不起我的错。

标签: c# .net winforms datagridview


【解决方案1】:

如果这是 winforms 循环遍历 DataGridView 的行,如下所示:

编辑:根据 Uwe Keim DBNull.Value 也应该检查。

for(int i = 0; i< DataGridView.Rows.Count; i++)
{
    DataGridViewCell colPhone1 = DataGridView.Rows[i].Cells["Phone1"];
    DataGridViewCell colPhone2 = DataGridView.Rows[i].Cells["Phone2"];
    if(colPhone1.Value == null || colPhone1.Value == DBNull.Value)
    {
        colPhone1.Value = colPhone2.Value;

    }
    else if(colPhone2.Value == null || colPhone2.Value == DBNull.Value)
    {
        colPhone2.Value = colPhone1.Value;
    }
}

【讨论】:

  • 也许还应该检查DBNull.Value and not just null?
  • 我认为你不需要检查 colPhone2 并且需要在复制时清除 colPhone2。我假设要求是将“空,数字”更改为“数字,空”,而不是重复为“数字,数字”。但你是对的,你可以修复数据
  • @HansKesting 根据要求编辑并更正了答案,感谢指出。
  • 对不起,我的回答迟了!它正在工作:) 谢谢
【解决方案2】:

一般的解决方案是使用CellFormatting 来满足这些要求。在事件处理程序中,您可以设置e.Value 或设置单元格的样式。

这里举个例子:

DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
    dt = new DataTable();
    dt.Columns.Add("Phone1");
    dt.Columns.Add("Phone2");
    dt.Rows.Add("123", "456");
    dt.Rows.Add(DBNull.Value, "789");
    dataGridView1.CellFormatting += DataGridView1_CellFormatting;
    dataGridView1.CellEndEdit += DataGridView1_CellEndEdit;
    dataGridView1.DataSource = dt;
}
private void DataGridView1_CellEndEdit(object sender, 
    DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 1 && e.RowIndex >= 0)
        dataGridView1.InvalidateCell(0, e.RowIndex);
}
private void DataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex >= 0)
    {
        if (e.Value == DBNull.Value)
        {
            var otherValue = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
            if (otherValue != DBNull.Value)
            {
                //If you want just display value of the other cell
                e.Value = otherValue;

                // If you want display and push value of other cell into this cell
                //((DataRowView)dataGridView1.Rows[e.RowIndex]
                //       .DataBoundItem)[e.ColumnIndex] = otherValue;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 2013-03-03
    • 2023-03-03
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多