Sunday, August 5, 2012

CDC Software Pivotal CRM 6.0: How to get acces to SearchView.SearchData.ResultsViewControl control.

We can't modify SearchView.SearchData.ResultsViewControl in a usual way because it has an internal get accessor.
But we can use reflection.

Here is an example how to change SearchView rows text color deppending on received data.

1. Add the following dll references to your project:
  • Pivotal.CommonControls.dll
  • CdcSoftware.Ios.UI.dll

2. Register "OnSearchCompleted" notification event handler:
[NotificationEvent("OnSearchCompleted", Table = "Support_Incident")]
public void OnSupportIncidentSearchCompleted(object notifyParams)
{
 ...
}

3. Get DataGridView control via reflection.
CdcSoftware.Pivotal.CommonControls.ResultsViewControl resultsViewControl = searchView.SearchData.GetType().GetProperty("ResultsViewControl").GetValue(searchView.SearchData, null) as CdcSoftware.Pivotal.CommonControls.ResultsViewControl;
if (resultsViewControl != null)
{
CdcSoftware.Pivotal.CommonControls.PivotalDataGridView dataGridView = resultsViewControl.GetType().GetField("_dataGridView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(resultsViewControl) as CdcSoftware.Pivotal.CommonControls.PivotalDataGridView;
}

4. Modify you want.

The full code example:
[NotificationEvent("OnSearchCompleted", Table = "Support_Incident")]
        public void OnSupportIncidentSearchCompleted(object notifyParams)
        {
            ISearchView searchView = ClientContext.ClientAPIService.GetActiveContentObject() as ISearchView;
            if (searchView != null)
            {
                //Get ResultsViewControl
                CdcSoftware.Pivotal.CommonControls.ResultsViewControl resultsViewControl = searchView.SearchData.GetType().GetProperty("ResultsViewControl").GetValue(searchView.SearchData, null) as CdcSoftware.Pivotal.CommonControls.ResultsViewControl;

                if (resultsViewControl != null)
                {
                    //Get DataGridView control
                    CdcSoftware.Pivotal.CommonControls.PivotalDataGridView dataGridView = resultsViewControl.GetType().GetField("_dataGridView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(resultsViewControl) as CdcSoftware.Pivotal.CommonControls.PivotalDataGridView;
                    
                    //Attach RowPostPaint event
                    dataGridView.RowPostPaint += new DataGridViewRowPostPaintEventHandler((object sender, DataGridViewRowPostPaintEventArgs e) =>
                    {
                        CdcSoftware.Pivotal.CommonControls.PivotalDataGridView pivotalDataGridView = sender as CdcSoftware.Pivotal.CommonControls.PivotalDataGridView;
                        if (dataGridView.Columns.Contains("Status_Id@Rn_Descriptor"))
                        {
                            //Color closed incidents
                            string columnTextValue = Convert.ToString(pivotalDataGridView.Rows[e.RowIndex].Cells["Status_Id@Rn_Descriptor"].Value);
                            if (columnTextValue.Contains("Closed"))
                            {
                                pivotalDataGridView.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                            }
                        }
                    });
                }
            }
        }

Regards,
Max Shafranski

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Max,

    Thanks for the code you posted regarding controlling SRL color.
    Would you explain what this Status_Id@Rn_Descriptor mean?
    I can change the forecolor, backcolor and etc but I can't apply a condiotion.

    Many Thanks

    ReplyDelete
  3. No worries. I found out. That's the column name

    ReplyDelete
  4. I appreciate this article for the well-researched content and excellent wording. I got so interested in this material that I couldn’t stop reading. Your blog is really impressive. Salesflow alternative

    ReplyDelete