2

So I have this DataGridView with two columns which are named "Phone1" and "Phone2".

The problem is when Phone1 is empty it can be that in Phone2 there is a phone number. How can I check in C# if Phone1 is empty but when Phone2 isn't, the text from Phone2 goes to Phone1?

Assuming the following input, which Phone1 doesn't have value in the second and third rows:

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

It's the expected output, which value of Phone2 has been shown as a fallback:

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

So, ho can I show value of the second column in the first column as a fallback if first column is empty?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
darby
  • 127
  • 8

2 Answers2

2

If this is winforms loop through the rows of your DataGridView like this:

Edit: As per Uwe Keim DBNull.Value should be checked also.

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;
    }
}
Keith
  • 276
  • 2
  • 14
  • Probably one should also check for [`DBNull.Value` and not just `null`](https://stackoverflow.com/q/4958379/107625)? – Uwe Keim Aug 29 '19 at 07:18
  • I don't think you need to check colPhone2 and need to clear colPhone2 when copied. I assume the requirement is to change "empty, number" to "number, empty" and not duplicate to "number, number". But you are right, you can just fix the data – Hans Kesting Aug 29 '19 at 09:41
  • @HansKesting Edited and corrected the answer based on the requirements, thanks for pointing it out. – Keith Aug 30 '19 at 01:49
  • Sorry for my late answer! It is working :) Thank you – darby Dec 17 '19 at 14:06
0

The general solution is using CellFormatting for these kind of requirements. In the event handler, you can set e.Value or set styles for the cell.

Here as an example:

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;
            }
        }
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398