简体   繁体   中英

How to current logged in user's id to textbox

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?

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.

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}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM