Click here to Skip to main content
14,735,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a WinForm with a datagrid in it. Inside the data grid, I have one column which is DataGridViewLinkColumn in which I will have hyperlink to allow users to navigate to website based on the URL constructed.
I have a few questions regarding this as I am not familiar with WinForm. Below are my codes:
foreach (DataGridViewRow row in dgvCustomer.Rows)
     foreach (var cell in row.Cells)
          DataGridViewLinkCell linkCell = cell as DataGridViewLinkCell;
          if (linkCell.Value.ToString()== "0")
              linkCell.UseColumnTextForLinkValue = true;
              linkCell.Value = "Use Customer Standard";                        

I have successfully populate all the data into gridview.
From the above codes, I will access the gridview row by row and I tried to change the behaviour and value of the particular cell which fulfill certain condition like above.
This is due to although this is DataGridViewLinkColumn, under certain condition, I want to remove the hyperlink behaviour and just display the value as plain text.
I always getting null exception at the linkCell variable, at the IF statement.
May I know how can I achieve this?

What I have tried:

1. Search for article on internet but to no avail.
2. Tried to make use of the intellisense in codes(putting the .(dot)) to see if there is any suggestion by VS but found none.
Updated 19-Oct-20 5:32am

if (linkCell.Value.ToString()== "0")

That is wrong, if the cell is empty then Value will be 'null'. Use:
if (linkCell.Value == null)
You could check If linkCell.Value IsNot Nothing
if (linkCell.Value.ToString()== "0")
Richard MacCutchan 19-Oct-20 10:33am
Not in C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900