user2827958
user2827958

Reputation: 357

Column from selected Datagrid

I have code that returns me the selected row from a datagrid. Now i want to have the value of the 3th column. The code I have below already gives me the selected row

private void BandGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var row_list = GetDataGridRows(BandGrid);
                foreach (DataGridRow single_row in row_list)
                {
                    if (single_row.IsSelected == true)
                    {

                    }
                }

            }
            catch { }
        }

Upvotes: 1

Views: 147

Answers (2)

Felix Castor
Felix Castor

Reputation: 1675

Assuming that your DataGrid has an underlying data structure and you are not using datagridview, each row represents an object usually in a list of objects. You can just cast the selected row to the object's Type and pull the field of the cell you want. Also you don't have to loop through each one in the list. SelectedItem will have what you want.

Edited

private void BandGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Band single_row = (Band)BandGrid.SelectedItem;

        string cellValue = single_row.Picture;

    }

Edited End

If you have multi select feature on you may need to pull all iterating through SelectedItems. Note: don't make changes to the items in the foreach loop this will cause errors. You will need to make a copy of the data if you need to change the data.

 private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      listCells = new List<string>();
       foreach(MyClass single_row in BandGrid.SelectedItems)
        {

              //do something with the object
             listCells.add( single_row.Picture);
        } 

    }

Example program. This sets the DataSource for the grid to List<MyClass> and every time the selection is changed textbox1 displays data in column c from the selected row.

   public partial class MainWindow : Window
    {
      public class MyClass
      {
       public int a { get; set; }
       public int b { get; set; }
       public int c { get; set; }
       public int d { get; set; }
      }
      public MainWindow()
      {
        InitializeComponent();
        MyClass obj;
        List<MyClass> bind = new List<MyClass>();
        for (int i = 0; i < 10; i++)
        {
            obj = new MyClass();
            obj.a = i;
            obj.b = 2*i;
            obj.c = 3*i;
            obj.d = 4*i;
            bind.Add(obj);
        }

        dataGrid1.ItemsSource = bind;
    }

    private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        textBox1.Text = ((MyClass)dataGrid1.SelectedItem).c.ToString();



    }
}

Here's the xaml

<Window x:Class="yo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="116,116,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="344" SelectionChanged="dataGrid1_SelectionChanged" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="87,41,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>

Upvotes: 1

SpiderCode
SpiderCode

Reputation: 10122

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
       var selectedValue  = GridView1.SelectedRow.Cells[2].Text;
}

Upvotes: 0

Related Questions