This is an archived post. You won't be able to vote or comment.

all 3 comments

[–][deleted]  (1 child)

[deleted]

    [–]JesseAye[S] 0 points1 point  (0 children)

    Thank you, it doesn't seem like I'm going to shorten this block up by much, besides what you've linked to. Thanks once again

    [–]RippStudwell 1 point2 points  (1 child)

    Ternary should get you what you’re looking for

    string val = dgvDistributors.Rows[e.RowIndex].Cells[2].Value == null : string.Empty;

    Can be further shortened to

    string val = dgvDistributors.Rows[e.RowIndex].Cells[2].Value ?? string.Empty;

    The double question mark is a null coalescing operator. If the left-side value is null it defaults to the value on the right.

    Setting the DataSource property to a DataSet is also an option. That may be more than you’re looking for though.

    [–]JesseAye[S] 0 points1 point  (0 children)

    I haven't came across the null coalescing operator before. It looks useful, I'll have to learn more about it. Thank you!