Add unbound column based on other column value

Hi,

Want to add an unbound column based on another Datagrid column.


Start DateEnd DateUnbound Column
1-Mar-2131-Mar-22 Active
1-Mar-2115-Apr-21Need Action
1-Mar-2120-Mar-21Expired


In the above example, want to add an unbound column based on the below conditions

  1. If (Current Date - End Date) > 30 days then "Active"
  2. If (Current Date - End Date) <= 30 days and If (Current Date - End Date) >= 0 then "Need Action"
  3. If (Current Date - End Date) < 0 days then "Expired"
Need help on urgent basis.


6 Replies 1 reply marked as answer

MA Mohanram Anbukkarasu Syncfusion Team March 23, 2021 06:38 AM UTC

Hi RAAHUL, 

Thanks for using Syncfusion products.  

You can achieve your requirement by using QueryUnboundColumnInfo event as shown in the following code example.  

Code example :  

this.sfDataGrid1.Columns.Add(new GridUnboundColumn() 
{ 
    MappingName="UnboundColumn1" 
}); 
 
private void SfDataGrid1_QueryUnboundColumnInfo(object sender, Syncfusion.WinForms.DataGrid.Events.QueryUnboundColumnInfoArgs e) 
{ 
    if (e.Column.MappingName == "UnboundColumn1") 
    { 
        var currentDate = DateTime.Now; 
        var endDate = (e.Record as DataModel).EndDate; 
 
        if ((currentDate - endDate).Days > 30) 
            e.Value = "Active"; 
        else if ((currentDate - endDate).Days <= 30) 
            e.Value = "Need Action"; 
        else if ((currentDate - endDate).Days < 0) 
            e.Value = "Expired"; 
    } 
} 



Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 



RA RAAHUL March 23, 2021 09:15 AM UTC

Hi Mohanram,

Thanks for the update.

Kindly note, Startdate and EndDate columns are mapped to the database and added columns using the Column collections property.
In this case how I can access data of the EndDate column. 

Have tried the below code as per UG Reference. However, getting error.

  Private Sub dgvContract_QueryUnboundColumnInfo(sender As Object, e As QueryUnboundColumnInfoArgs) Handles dgvContract.QueryUnboundColumnInfo

        If e.Column.MappingName = "Status" Then
            Dim currentDate = DateTime.Now
            Dim endDate = Convert.ToDateTime(e.Record.GetType().GetProperty("EndDate").GetValue(e.Record))

            If (currentDate - endDate).Days > 30 Then
                e.Value = "Active"
            ElseIf (currentDate - endDate).Days <= 30 Then
                e.Value = "Need Action"
            ElseIf (currentDate - endDate).Days < 0 Then
                e.Value = "Expired"
            End If
        End If
    End Sub

Error:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=In_Mgmnt
  StackTrace:
   at In_Mgmnt.frmContacts.dgvContract_QueryUnboundColumnInfo(Object sender, QueryUnboundColumnInfoArgs e) in \\Vb.Net Project\Projects\In_Mgmnt\In_Mgmnt\Forms\frmContacts.vb:line 294




RA RAAHUL March 24, 2021 06:43 AM UTC

Hi,

Awaiting solution ASAP.

Regards,
Raahul


MA Mohanram Anbukkarasu Syncfusion Team March 24, 2021 12:51 PM UTC

Hi RAAHUL, 

Thanks for the update.  

You can resolve this by using the below given code example to get the value.  

Code example:  

dataGrid.View.GetPropertyAccessProvider().GetValue(e.Record, “EndDate”);  

Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 


Marked as answer

RA RAAHUL March 24, 2021 02:56 PM UTC

Hi Mohanram,

Thanks for the solution.
It works fine. However, the application is getting crashed when the user clicks an unbound column for sorting.

Kindly help.

Regards,
Raahul


MA Mohanram Anbukkarasu Syncfusion Team March 25, 2021 02:03 PM UTC

Hi RAAHUL, 

Thanks for the update.  

Kindly share the stack trace details of the reported exception that occurs while sorting the unbound column. It will be more helpful for us to find the exact cause for the problem and to provide a prompt solution at earlier.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon