DataGridHyperlinkColumn Question

Posted by: scottadelong on 21 January 2018, 11:39 am EST

    • Post Options:
    • Link

    Posted 21 January 2018, 11:39 am EST

    I have a C1DataGrid bound to a DataTable. I would like one of the columns to be a DataGridHyperlinkColumn, but instead of, for example, having its click event open a URL in a web browser, I would like it to call a method within the application instead. In a manner similar to the code I’ve included below. Is this possible and if so, how? Thank you.

    public class SomeDataGrid : C1DataGrid

    {

    public SomeDataGrid(DataTable someDataTable)

    {

    this.AutoGenerateColumns = false;

            this.ItemsSource = someDataTable.DefaultView;
    
            string someDataColumn = "someDataColumn";
    
            C1.WPF.DataGrid.DataGridHyperlinkColumn column = new C1.WPF.DataGrid.DataGridHyperlinkColumn();
    
            Binding binding = new Binding();
            binding.Path = new PropertyPath(someDataColumn);
            binding.Mode = BindingMode.OneWay;
            binding.Source = someDataTable.DefaultView;
    
            column.Click += (s, e) =>
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
    
                string someInfo = drv[SqlTxt._App_Object_GUID].ToString();
    
                SomeClass.SomeMethod(someInfo);
            };
    
            this.Columns.Add(column);
        }
    }
    
    public class SomeClass
    {
        public static void SomeMethod(string someInfo)
        {
            // Do Something ...
        }
    }
    
  • Posted 21 January 2018, 3:28 pm EST

    Hi,

    I suggest you to use DataGridTemplateColumn for making a custom template column as below:

    <c1:DataGridTemplateColumn Header="Mail To" HorizontalAlignment="Center">
                        <c1:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Name="textblock" HorizontalAlignment="Center"  VerticalAlignment="Center" >
                                    <Underline>
                                      <Run Text="{Binding URL}" MouseLeftButtonDown="Run_MouseLeftButtonDown">                                     
                                      </Run>
                                    </Underline>
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Setter Property= "Foreground" Value="Blue"/>
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Border.Cursor" Value="Hand"/>
                                                    <Setter Property="Foreground" Value="Red"></Setter>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </DataTemplate>
                        </c1:DataGridTemplateColumn.CellTemplate>
                    </c1:DataGridTemplateColumn>
    

    Write your mail sending code in an event:

    private void Run_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                // Write your code here to send mail
            }
    

    Hope, it will help you.

    Thanks,

    Singh

    LinkToOpenDataGrid.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels