Click here to Skip to main content
15,889,723 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:
C#
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.
Posted
Updated 19-Oct-20 4:32am

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

That is wrong, if the cell is empty then Value will be 'null'. Use:
C#
if (linkCell.Value == null)
 
Share this answer
 
You could check If linkCell.Value IsNot Nothing
before
if (linkCell.Value.ToString()== "0")
 
Share this answer
 
Comments
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