【问题标题】:How to make a column (if not first column) of a DataGrid into Hyperlink如何将 DataGrid 的列(如果不是第一列)变成超链接
【发布时间】:2018-03-21 05:05:23
【问题描述】:

我很震惊,这听起来很简单,但我很困惑。我有一个 DataGrid 控件,它在运行时从 SQL Server 填充数据。我需要的只是将 ID 列变成超链接(我也尝试将颜色更改为蓝色但没有成功)

 private void fillDataGrid(string strSQL)
    {

        try
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = KaskoConnectionString.ConnectionString;
            con.Open();
            sda = new SqlDataAdapter(strSQL, con);
            ds = new DataSet();
            sda.Fill(ds);
            DGHolder.ItemsSource = ds.Tables[0].DefaultView;
            //DGHolder.Columns[0].

        }
        catch (Exception ex)
        {
            this.ShowMessageAsync("Error", ex.Message, MessageDialogStyle.Affirmative);
        }

XAML 非常基础,如下所示

  <DataGrid Name="DGHolder" BorderBrush="Gray" BorderThickness="4"  Background="LightGray" AlternatingRowBackground="LightBlue" AutoGenerateColumns="True" Style="{StaticResource AzureDataGrid}" Grid.Column="0"  FontWeight="Bold"  MouseLeftButtonUp="DGHolder_MouseLeftButtonUp">

            </DataGrid>

任何帮助将不胜感激...

非常感谢

【问题讨论】:

    标签: c# sql sql-server wpf datagrid


    【解决方案1】:

    这个呢?

    代码:

    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Documents;
    
    namespace WpfApp1
    {
        public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
    
                DataContext = new[]
                {
                    new Example {Company = "Google", Url = "http://www.google.com"},
                    new Example {Company = "Amazon", Url = "http://www.amazon.com"}
                };
            }
    
            private void HyperlinkClick(object sender, RoutedEventArgs e)
            {
                var link = (Hyperlink) e.OriginalSource;
                Process.Start(link.NavigateUri.AbsoluteUri);
            }
        }
    
        internal class Example
        {
            public string Company { get; set; }
            public string Url { get; set; }
        }
    }
    

    XAML:

    <Window
        x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpfApp1="clr-namespace:WpfApp1"
        mc:Ignorable="d">
    
        <Grid>
            <DataGrid
                d:DataContext="{d:DesignInstance wpfApp1:Example}"
                AutoGenerateColumns="False"
                ItemsSource="{Binding}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Company}" Header="Company" />
                    <DataGridHyperlinkColumn Binding="{Binding Url}" Header="Url">
                        <DataGridHyperlinkColumn.ElementStyle>
                            <Style>
                                <EventSetter Event="Hyperlink.Click" Handler="HyperlinkClick" />
                            </Style>
                        </DataGridHyperlinkColumn.ElementStyle>
                    </DataGridHyperlinkColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    

    这里需要超链接处理程序,因为它托管在 Window 中,如果它托管在 NavigationWindow 中,那么 it's automatic

    【讨论】:

      【解决方案2】:

      您可以处理AutoGeneratingColumn 事件:

      <DataGrid Name="DGHolder" ... AutoGeneratingColumn="dg_AutoGeneratingColumn">
      

      private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
      {
          if (e.PropertyName == "ID")
          {
              e.Column = new DataGridHyperlinkColumn()
              {
                  ContentBinding = new Binding(e.PropertyName),
              };
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-16
        • 1970-01-01
        • 1970-01-01
        • 2013-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多