Sunday, January 2, 2011

CDC Software Pivotal CRM 6.0: How to change rows style in PivotalSearchResultGrid

Here is an example we can access to PivotalSearchResultsGrid rows.
Lets color all rows that contain specific Company_Name value.
First of all we need to add DataBindingComplete event handler to the PivotalSearchResultsGrid control.
To do it we need to override OnFormInitialized method that fires after the form has been drawn and all the data has been loaded and bound:

public override void OnFormInitialized()
{
    PivotalSearchResultsGrid mySearchResultsGrid =   this.FormControl.GetControlByName("CompaniesPivotalSearchResultsGrid") as PivotalSearchResultsGrid;
    if (mySearchResultsGrid != null)
    {
        //Get a grid control from the PivotalSearchResultsGrid
        DataGridView grid = mySearchResultsGrid.ResultsViewControl.Controls[0].Controls[0] as DataGridView;
        //Handle DataBindingComplete event that fires when the content DataSource changed
        grid.d.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
    }
}

Than just implement the dataGridView1_DataBindingComplete event handler:

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    DataGridView grid = sender as DataGridView;
    for (int i = 0; i < grid.Rows.Count; i++ )
    {
        DataGridViewRow row = grid.Rows[i];
        //Access to row data
        string companyName = row.Cells["Company_Name"].Value.ToString();
        if (companyName == "Microsoft")
        {
            //Paint row in Blue
            row.DefaultCellStyle.BackColor = Color.Blue;
        }
    }
}

Thats all.
Enjoy with Pivotal 6.0.

3 comments:

  1. So I read somewhere that ResultsViewControl was deprecated but of course there was no mention of an alternate approach. Do you still use this approach?

    ReplyDelete
  2. So in your example above if "Company Name" is a field in the SRL that has its visibility set to "Dataset Only" it looks to me like it cannot be accessed. Is this true or am I doing something wrong?

    ReplyDelete
  3. Hi,
    You can access the row data-bound object to read Dataset Only columns:

    DataGridViewRow row = grid.Rows[i];
    var dataRowView = row.DataBoundItem as DataRowView;
    var value = dataRowView.Row["DataSetOnlyColumn"];

    ReplyDelete