Articles in this section
Category / Section

How to perform the copy or paste in the foreign key reference column in WinForms GridGroupingControl?

1 min read

Copy paste operation with foreign key

By default, the displayed cell value can be copied and pasted in any specified cell. In foreign key cell types, the copied values are the specific value members that have a foreign key reference of an object type. For example, while pasting the string type cell values to the foreign key cell and its foreign key is in the type of integer, the FormatException is thrown.

In order to copy-paste a cell value with a foreign key member field, a separate hash table instance can be used to store and retrieve the cell value from the foreign key cells and the corresponding display member values will be pasted in the required cell. This can be done through the PasteCellText event in the TableControl property.

C#

// Declared globally
Hashtable hTable = new Hashtable();
// Form()
// Trigger the required event.
this.gridGroupingControl1.TableModel.PasteCellText += new GridPasteCellTextEventHandler(TableModel_PasteCellText);
// Form()
foreach (DataRow row in lookUpDataTable.Rows)
{
   if (!hTable.Contains(row["CustomerName"]))
       hTable.Add(row["CustomerName"], row["CustomerID"]);
}
void TableModel_PasteCellText(object sender, GridPasteCellTextEventArgs e)
{
   if (e.Style.CellType == "ForeignKeyCell")
   {
      e.Cancel = true;
      GridTableCellStyleInfo style = this.gridGroupingControl1.TableControl.GetTableViewStyleInfo(e.RowIndex, e.ColIndex);
      Record rec = style.TableCellIdentity.DisplayElement.GetRecord();
      rec.SetValue("Customer", hTable[e.Text].ToString());
   }
}

 

 

 

VB

' Declared globally
Dim hTable As New Hashtable()
' Form()
' Trigger the required event.
AddHandler gridGroupingControl1.TableModel.PasteCellText, AddressOf TableModel_PasteCellText
' Form()
For Each row As DataRow In lookUpDataTable.Rows
   If Not hTable.Contains(row("CustomerName")) Then
    hTable.Add(row("CustomerName"), row("CustomerID"))
   End If
Next row
void TableModel_PasteCellText(Object sender, GridPasteCellTextEventArgs e)
   If e.Style.CellType Is "ForeignKeyCell" Then
   e.Cancel = True
   Dim style As GridTableCellStyleInfo = Me.gridGroupingControl1.TableControl.GetTableViewStyleInfo(e.RowIndex, e.ColIndex)
   Dim rec As Record = style.TableCellIdentity.DisplayElement.GetRecord()
   rec.SetValue("Customer", hTable(e.Text).ToString())
   End If

 

Screenshot

Sort operation based on the member

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