0

I am trying to get the Row's particular column from selected item in wpf from DataGrid.

Name of DataGrid is Datagrid_Newsale.

I am getting alert of whole row when it is selected, So i tried mapping its column.

Say if row is-

{ ID = 3, CustomerName = xyz, SaleDate = 05.08.2013 00:00:00, TotalAmount = 10 }

Then it's column CustomerName=xyz is to be shown in textbox.

Getting row-

var copyitem = Datagrid_NewSale.SelectedItem;

if (copyitem == null)
{
    MessageBox.Show("Please select values from list");
}
if (copyitem != null)
{                       
    MessageBox.Show(copyitem.ToString());
}

For getting customerName into text box i tried creating a new instance of model-

public class CustomerDetailes
{
    public string CustomerName { get; set; }
}

And values from database from Customer Table-

public void viewcustomername()
{
    List<CustomerDetailes> ilist = null;
    ilist = (from order in db.Customer
                select new CustomerDetailes
                {
                    CustomerName= order.CustomerName
                }).ToList();
    txtCustumer.Text = ilist.ToString();

}

So giving it one more try-

CustomerDetailes copyitem = (CustomerDetailes)Datagrid_NewSale.SelectedItem;

if (copyitem == null)
{
    MessageBox.Show("Please select values from list");
}
if (copyitem != null)
{                       
    MessageBox.Show(copyitem.ToString());
}

txtCustomer.text=copyitem.CustomerName;  //CustomerName into a textbox

But it is referencing null in copyitem.

How can I get particular column from the whole row.

Yael
  • 1,566
  • 3
  • 18
  • 25
Manoz
  • 6,507
  • 13
  • 68
  • 114
  • @nit, Yes getting whole row as it is described in question. – Manoz Aug 29 '13 at 13:17
  • @nit, No.., For first try without using model `CustomerDetails`; it is showing full row. But when i used model then it is coming null. – Manoz Aug 29 '13 at 13:24
  • sorry i did't understand. Did you set the itemsSource of DataGrid to CustomerDetails collection? – Nitin Aug 29 '13 at 13:29
  • @nit, No I don't know about that, Maybe you wanna post an answer on that. – Manoz Aug 29 '13 at 13:32

4 Answers4

0

You will have to bind the ItemsSource of DataGrid to CustomerDetails collection in order to get CustomDetails in SelectedItem.

Create property in your viewmodel (if using MVVM) or in code behind like

List<CustomerDetails> customerDetails = new List<CustomerDetails>();
    List<CustomerDetails> MyCollection
    {
        get
        {

            return myList;
        }
        set
        {
            myList = value;
            PropertyChanged(this, new PropertyChangedEventArgs("MyCollection"));
        }
    }

and in xaml just do.

<DataGrid ItemsSource="{Binding MyCollection}"/>

OR if you are directly filling the Items in the datagrid add instances of CustomerDetails like

dataGrid.Items.Add(new CustomerDetails(){Name = "abc"}, xyz propertis)

Thanks

Nitin
  • 18,344
  • 2
  • 36
  • 53
0

If you can access the grid from your selection event then following should give your the column ((DataGrid)sender).CurrentCell.Column.Header

and use some mapping for the column name to the property of the object your want to show

AsitK
  • 651
  • 1
  • 4
  • 14
0

I came up with this easy solution.

Mapped datatype of copyitem that was Anonymous in my case. In this case using Dynamic datatype solved my problem.

Due to my data was coming dynamically and then i was trying to map out particular column, so it was not really possible to do it statically because there is not data then.

Using Dynamic Datatype-

 dynamic copyitem = dataGrid1.SelectedItem;

Accessing property-

int localId = copyitem.ID;

furthermore for customerName,TotalAmount i did the same.

Linq Query changes-

var query= (from order in db.Customer
where order.ID=localId
select order).ToList();

DataGrid_OpenSale.ItemsSource=query // returning data to another datagrid.

Manoz
  • 6,507
  • 13
  • 68
  • 114
0

in VB ⠀⠀⠀

Private Sub SCUSTDataGrid_GotFocus(sender As Object, e As RoutedEventArgs) Handles SCUSTDataGrid.GotFocus
  Dim og As DataGridCell = e.OriginalSource
  Dim ccontent As TextBlock = og.Content
  Dim dg As DataGrid
  dg = e.Source
  Dim selcol As String = dg.CurrentCell.Column.Header.ToString

  MessageBox.Show(selcol.ToString + ccontent.Text + " got focus event")
End Sub
karel
  • 5,489
  • 46
  • 45
  • 50