Create an Unbound Column with Hyperlink in Devexpress WinForm GridControl

|
| By Harleen Kaur

When manually writing the code for creating a column data source for Devexpress GridControl instead of using the designer window in WinForm Application, one needs to use properties and events in the correct way while using unbound data columns.

The sample code below uses the AddRange method of a specific view of GridControl. If we are using bound columns in column source for Devexpress GridControl, assigning column source to the view of grid control is simple as follows:

myGridView.Columns.AddRange(new GridColumn[] {
new GridColumn()
{
FieldName="Company",
Caption = "Name",
Visible = true,
VisibleIndex=2,
Width=150,
OptionsColumn={
AllowEdit = false,
},
},

new GridColumn()
{
Caption = "Category",
FieldName = "Category",
Visible = false,
VisibleIndex=1,
Width=55 ,
OptionsColumn={
AllowEdit = false,
},

},
…….
}

But if I want to add an unbound column that makes use of two or more field values from a data source depending on its type, the UnboundExpression property is required to calculate the value or display the formatted string.

Example of string type –

{
FieldName = "PrimaryContact",
Caption = "Primary Contact",
UnboundDataType = typeof(string),
UnboundExpression = "[ColumnFieldName1]+' '+[ColumnFieldName1]"
}

Assigning a Field name is required, even though the column is unbound, otherwise, it will not display the text correctly.

Suppose I want to join the two column values: company address and pin code, it can be done using Unbound Expression as “[address ]+’, PinCode: ‘+[pincode]“.

But to create a string type unbound data column as a hyperlink with a customized link click, we need the RepositoryItemHyperLinkEdit repository item to be added to the grid column.

DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit HyperLinkUnbound =
new DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit()
{
Name = “HyperLinkUnbound”,
SingleClick = true,

};
HyperLinkUnbound.OpenLink += (sender, e) => {
HyperLinkUnbound _Click(sender, e); return;
};

myGridControl.RepositoryItems.Add(HyperLinkStepTwo);

Events like ButtonClick will not work for Hyperlink
The Event can be handled in the format as follows:

private void HyperLinkUnbound _Click(object sender, OpenLinkEventArgs e)
{
// do whatever you want here
}

The structure for adding unbound column along with bound columns will be as follows:

gridViewContacts.Columns.AddRange(new GridColumn[] {
Bound columns …..,
new GridColumn()
{

FieldName = "MyCustomField",
Caption = "My Custom Field",
OptionsColumn={
AllowEdit = true,
ReadOnly= true
},
ColumnEdit= HyperLinkUnbound,
Visible = true,
VisibleIndex = 3,
Width = 150,
UnboundDataType = typeof(string),
UnboundExpression = "[ColumnFieldName1]+' '+[ColumnFieldName1]"
},
Bound columns …..
}

And further, suppose we want to make the Unbound column a Hyperlink

Note=> If GridControl editable property is set to false, the hyperlink event will not work, instead, set the individual column’s property “AllowEdit” to false and for the Unbound column set AllowEdit = true,
and ReadOnly= true.

Leave a Reply

Your email address will not be published. Required fields are marked *