How to perform the copy or paste in the foreign key reference column in WinForms GridGroupingControl?
Copy paste operation with foreign key
By default, the displayed cell value can be copied and pasted into any specified cell. In foreign key cell types, the copied values are the specific value members that have a foreign key reference to an object type. For example, while pasting the string-type cell values to the foreign key cell and its foreign key is of the type 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();
// Trigger the required event.
this.gridGroupingControl1.TableModel.PasteCellText += new GridPasteCellTextEventHandler(TableModel_PasteCellText);
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());
}
}' Declared globally
Dim hTable As New Hashtable()
' Trigger the required event.
AddHandler gridGroupingControl1.TableModel.PasteCellText, AddressOf TableModel_PasteCellText
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
Private Sub TableModel_PasteCellText(ByVal sender As Object, ByVal e As GridPasteCellTextEventArgs)
If e.Style.CellType = "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
End Sub