0

Im trying to get data from first row in datagrid in c# wpf app but i cannot figure out the code.

Can somebody help me?

I tried WF app code but it doesn't work because there's not function of .Rows

return Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index]
          .Cells[0].Value.ToString());

Thanks

UPDATE:

Im trying to get current's logged in users id to display in textbox, so i can use it to make a reservation later and join two tables.

Also can somebody help write SQL statement that joins two different id's (user's ID and id of the book that user is buying into a new table called "reservations"

Can somebody help me?

fkr
  • 155
  • 3
  • 9
  • 21
  • 1
    Please provide more information about the data in the DataGrid. What are you using for your ItemsSource data binding? – Ryan Mar 27 '14 at 16:32
  • it is strongly recommended that you use proper [DataBinding](http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) as opposed to procedural stuff in WPF. Your code is highly prone to runtime errors due to the `Convert` stuff and `.ToString()` and all that. – Federico Berasategui Mar 27 '14 at 16:34
  • Im trying to get the ID of the User i select – fkr Mar 27 '14 at 16:37
  • @GrantWinney im not, thats why im here – fkr Mar 27 '14 at 16:38

2 Answers2

2

Since others have already posted a potential workaround for your problem, I'll go ahead and explain the proper solution:

Delete all your code and start all over.

WPF is NOT winforms, and if you're working with WPF, you need to leave behind any and all notions you got from the traditional approach and understand and embrace The WPF Mentality.

Basically, you don't "get data from a DataGrid" in WPF, simply because UI is NOT Data.

This means that the data you're trying to obtain must not be stored by the UI to begin with. Instead, you need to create a proper Data Model and use DataBinding to populate the UI with such data from the DataModel.

The responsibility of the UI is to show data, not store it. This is an important mindshift from the traditional approach where you would manually populate the UI with data and then retrieve the data from the UI.

WPF Two-way DataBinding capabilities make it easier to implement such scenarios in a clean, decoupled way.

Say you have a User class which has a Name and an Id property:

public class User
{
    public int Id {get;set;}

    public string Name {get;set;}
}

first step to show this in a DataGrid is to create a proper ViewModel that contains a collection of this class:

public class ViewModel
{
    public ObservableCollection<User> Users {get; private set;}

    public ViewModel()
    {
        Users = new ObservableCollection<User>();

        //... Populate the collection here.
    }
}

then you will use this class as the DataContext of your UI:

//Window's constructor
public MainWindow()
{
   InitializeComponent();

   //Here we set the DataContext:
   DataContext = new ViewModel();
}

Finally, you create the proper DataBindings in XAML:

<DataGrid ItemsSource="{Binding Users}"
          x:Name="DataGrid"/>

<!-- ... -->

<TextBox Text="{Binding SelectedItem.Id, ElementName=DataGrid}"/>

Notice how I'm using an ElementName binding to bind the TextBox directly to the DataGrid.

This is the preferred, professional approach to WPF. I suggest that you read all the linked documentation and try to familiarize yourself more with this approach.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
0

If you're populating your DataGrid with a List<SomeObject>, for example, try this:

var row = ((SomeObject)DataGrid1.Items[0]);

Just cast the item back to your class type, then access the properties on it. You may want to do a check to make sure there's something displayed in the grid before accessing Items[0].

You can sort different columns in your grid, and this will always grab the record displayed at the top.


Edit, after seeing the following comment:

When I click on first row in DATAGRID, this function should/must display user's ID in the textbox below data grid. That is all.

You can bind directly to your grid, to display the property you want from the currently selected row.

<DataGrid x:Name="DataGrid1" />

<TextBox Text="{Binding ElementName=DataGrid1, Path=SelectedItem.UserId}" />
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • this is just used to return the user ID in the textbox – fkr Mar 27 '14 at 16:58
  • When I click on first row in DATAGRID, this function should/must display user's ID in the textbox below data grid. That is all – fkr Mar 27 '14 at 17:00
  • Ok thanks this works. Is there anyway to display Current user's ID in the textbox without reading it from the datagrid? – fkr Mar 27 '14 at 17:27
  • I'm trying to display current's logged in users ID without using DataGrid, because i cannot find a way to only display current logged in user's info in the datagrid. I only know how to display all the users data. Any help? – fkr Mar 27 '14 at 17:37