Articles in this section
Category / Section

How to retrieve the text that is set in the TableControlDrawCellDisplayText event of the WinForms GridGroupingControl?

3 mins read

Retrieving the display text

Problem

There is no direct way to retrieve the text that is set for any cell in the TableControlDrawCellDisplayText event because the GridStyleInfo object associated with the cell does not hold any information.

Solution

The text can be retrieved by having a hashtable with record and the column names as the hashcode key. In the following code example, a unique key for each cell is generated because the row index might be changed due to other functions. For example, Sorting. The GetKey method is used to convert the record and column names to hashcode key.

C#

//Gets the particular cell value.
private object GetKey(Record record, string fieldname)
{
    string key = record.Id.ToString() + fieldname;
    return key;
}

VB

'Gets the particular cell value.
Private Function GetKey(ByVal record As Record, ByVal fieldname As String) As Object
    Dim key As String = record.Id.ToString() & fieldname
    Return key
End Function

A column QtyPerUnit is customized by the TableControlDrawCellDisplayText event to format the cell value to Kilograms. For example, 1000 is changed to 1 Kg. These customized texts are stored into the hashtable based on its hashcode key.

C#

//Creates the hash table to get the current values of the cell while sorting by using the key.
Hashtable ht = new Hashtable();
//Handles the display text differently from the inner text.
void gridGroupingControl1_TableControlDrawCellDisplayText(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlDrawCellDisplayTextEventArgs e)
{
    GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex);
    GridTableCellStyleInfoIdentity id = style.TableCellIdentity;
    //Checks whether the column name is QtyPerUnit or not.
    if (id.DisplayElement.Kind == DisplayElementKind.Record && id.Column != null && id.Column.Name == "QtyPerUnit")
    {
        //Converts the cellvalue into double format.
        double value = double.Parse(e.Inner.Style.CellValue.ToString());
        //Formats the value.
        string text = (value / 1000).ToString() + " Kg";
        //Gets the records.
        Record rec = style.TableCellIdentity.DisplayElement.GetRecord();
        //Points the current index after sorting also.
        object key = GetKey(rec, style.TableCellIdentity.Column.Name);
        ht[key] = text;
        //Displays the formatted text.
        e.Inner.DisplayText = text;
    }
}
//Gets the particular cell value.
private object GetKey(Record record, string fieldname)
{
    string key = record.Id.ToString() + fieldname;
    return key;
}

VB

'Creates the hash table to get the current values of the cell while sorting by using the key.
Private ht As New Hashtable()
'Handles the display text differently from the inner text.
Private Sub gridGroupingControl1_TableControlDrawCellDisplayText(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlDrawCellDisplayTextEventArgs)
    Dim style As GridTableCellStyleInfo = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex)
    Dim id As GridTableCellStyleInfoIdentity = style.TableCellIdentity
    'Checks whether the column name is QtyPerUnit or not.
    If id.DisplayElement.Kind Is DisplayElementKind.Record AndAlso id.Column IsNot Nothing AndAlso id.Column.Name = "QtyPerUnit" Then
        'Converts the cellvalue into double format.
        Dim value As Double = Double.Parse(e.Inner.Style.CellValue.ToString())
        'Formats the value.
        Dim text As String = (value / 1000).ToString() & " Kg"
        'Gets the records.
        Dim rec As Record = style.TableCellIdentity.DisplayElement.GetRecord()
        'Points the current index after sorting also.
        Dim key As Object = GetKey(rec, style.TableCellIdentity.Column.Name)
        ht(key) = text
        'Displays the formatted text.
        e.Inner.DisplayText = text
    End If
End Sub
'Gets the particular cell value.
Private Function GetKey(ByVal record As Record, ByVal fieldname As String) As Object
    Dim key As String = record.Id.ToString() & fieldname
    Return key
End Function

Finally, the value of the selected records is retrieved and the customized text is recovered through the hashtable ht[GetKey(record, "QtyPerUnit")]. These changes are handled in the buttonClick event.

C#

private void btn_retrieve_Click(object sender, EventArgs e)
{
string result = string.Empty;
//get the selected records
foreach (SelectedRecord rec in this.gridGroupingControl1.Table.SelectedRecords)
{
    Record record = rec.Record;
    result += "\nProduct = " + record.GetValue("Product").ToString() + "\t -> QtyPerUnit = " + ht[GetKey(record, "QtyPerUnit")].ToString() + "\t -> UnitPrice = " + record.GetValue("UnitPrice").ToString()+"\n";
}
this.richTextBox1.Text = result;
//MessageBox.Show(result);
}

VB

Private Sub btn_retrieve_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim result As String = String.Empty
'Gets the selected records.
    For Each rec As SelectedRecord In Me.gridGroupingControl1.Table.SelectedRecords
        Dim record As Record = rec.Record
        result &= Constants.vbLf & "Product = " & record.GetValue("Product").ToString() & Constants.vbTab & " -> QtyPerUnit = " & ht(GetKey(record, "QtyPerUnit")).ToString() & Constants.vbTab & " -> UnitPrice = " & record.GetValue("UnitPrice").ToString()+Constants.vbLf
    Next rec
    Me.richTextBox1.Text = result
    'MessageBox.Show(result);
End Sub

After applying the properties, the Grid looks like the following screenshot.

Show the retrieve text in GridGroupingControl

Samples:

C#: Retrieving_DisplayText

VB: Retrieving_DisplayText

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied