srn
srn

Reputation: 89

How to read specific columns from Gridview.Items in WPF?

I have inserted multiple urls into the GridView column URL. Now I want to read all the urls from the column URL. I'm not sure how to open the urls one by one from the column URL. I tried loading the {Binding URL}, but it wasn't possible.

Is there a way to read the texts from GridView column URL? or do i have to try a different approach?

Below is the XAML:

<Grid RenderTransformOrigin="0.5,0.5">

    <Grid.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform/>
            <TranslateTransform/>
        </TransformGroup>

    </Grid.RenderTransform>

    <Label x:Name="label1" Content="URL :" HorizontalAlignment="Left" Margin="22,27,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="146" Margin="64,30,0,0" TextWrapping="Wrap" Text="Enter URLs Here" VerticalAlignment="Top" Width="555" AcceptsReturn="True"/>
    <Button x:Name="button1" Content="Load" HorizontalAlignment="Left" Margin="652,19,0,0" VerticalAlignment="Top" Width="134" Height="40" Click="Button1_Click"/>
    <Grid Margin="0,0,0,0.333">

        <DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Height="225" VerticalAlignment="Top" Width="761" Margin="22,185,0,0" AutoGenerateColumns="True">
            <DataGrid.Columns>

                <DataGridHyperlinkColumn Header="URL" Width="150" Binding="{Binding URL}"/>
                <DataGridHyperlinkColumn Header="Source" Width="150" Binding="{Binding Source}"/>

            </DataGrid.Columns>

        </DataGrid>

    </Grid>      

</Grid>

The Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;


namespace AIT
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            List<string> Urllines = textBox1.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (string strUrl in Urllines)
                dataGrid1.Items.Add(new { URL = strUrl});
        }

        private void ReadUrls()
        {
            foreach (string item in dataGrid1.Items)
                //item.URL???
        }
    }
}

Upvotes: 2

Views: 291

Answers (1)

Bizhan
Bizhan

Reputation: 17095

You can read items from DataGrid.Items like this:

foreach (var item in dataGrid1.Items)
{
    string url = (string)((dynamic)item).URL;
    //...
}

However, it is better to have a strongly typed approach. In which case you need to bind the ItemsSource of the DataGrid to an ObservableCollection<DataType> where DataType represents the data structure of each row. (e.g. it has a bindable URL property and so on)

Here's an example:

<DataGrid ItemsSource="{Binding ElementName=root, Path=Items}" ...>

where root is the x:Name of the window. and the following code is added to the windows's code behind:

    private ObservableCollection<RowVm> _items = new ObservableCollection<RowVm>();
    public ObservableCollection<RowVm> Items { get { return _items; } }

where RowVm is:

public class RowVm : DependencyObject
{
    public string URL
    {
        get { return (string)GetValue(URLProperty); }
        set { SetValue(URLProperty, value); }
    }
    public static readonly DependencyProperty URLProperty =
        DependencyProperty.Register("URL", typeof(string), typeof(RowVm), new PropertyMetadata(""));
    public string Source
    {
        get { return (string)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }
    public static readonly DependencyProperty SourceProperty =
        DependencyProperty.Register("Source", typeof(string), typeof(RowVm), new PropertyMetadata(""));

}

So instead of adding to dataGrid1.Items you will add to Items and read from Items

Upvotes: 1

Related Questions