简体   繁体   中英

How to access specific cells in datagridrow wpf C#

I have a datagrid where i use a foreach(DataGridRow gvr in myDataGrid) and I need to be able to take the info from specific cells in the row and put them into their respective class properties ie(a.MessageName = gvr.column["MessageName"].value.ToString()). But I haven't figured out how to get at the info based on the column. Here's what i have so far...

    foreach (DataGridRow gvr in dgAnnouncement.Items)
    {
         Announcement a = new Announcement();

         a.MessageName = gvr.Column["MessageName"].Value.ToString();
         a.Message = gvr.Column["Message"].Value.ToString();
     }

And here's my XAML...

<DataGrid AutoGenerateColumns="False" Height="631" ItemsSource="{Binding}" HorizontalAlignment="Left" SelectionMode="Single" SelectionUnit="FullRow" Margin="12,124,0,0" Name="dgAnnouncement" VerticalAlignment="Top" Width="976" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="MessageName"></DataGridTextColumn>
                        <DataGridTextColumn Header="Message"></DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>

After searching the web I still havent found a solution that works for me so thanks in advance for any help.

Ok so it sounds like you want to have a bound list of items that the user can add to and fill in values on

If you want to do this, the best way is via binding on your columns:

<DataGrid AutoGenerateColumns="False" Height="631" ItemsSource="{Binding}" HorizontalAlignment="Left" SelectionMode="Single" SelectionUnit="FullRow" Margin="12,124,0,0" Name="dgAnnouncement" VerticalAlignment="Top" Width="976" > 
    <DataGrid.Columns> 
        <DataGridTextColumn Header="MessageName" Binding="{Binding MessageName, Mode=TwoWay}"></DataGridTextColumn> 
        <DataGridTextColumn Header="Message" Binding="{Binding Message, Mode=TwoWay}"></DataGridTextColumn> 
    </DataGrid.Columns> 
</DataGrid> 

The question is - what do you have ItemsSource set to at the moment?

Ideally this should be a strongly typed collection of Appointment objects ( ObservableCollection<Appointment> maybe). Do you want the users to be able to add new rows? If so you need to either provide a button which performs the Add on the source collection or let the datagrid do it (I think it supports an 'empty row' where you can type values... though I usually use Telerik's RadGridView). Generally when you have a grid that has an empty row for the user to add a new value, the grid will look at the underlying collection that's bound and call the appropriate method to add an item. If this collection doesn't support an Add method (such as IBindingList does) I think the default is to create a new item using the parameterless constructor for the type (not too sure on this, might be worth doing some reading)

Basically, by binding these properties TwoWay it means that each item in the list can be edited by the user directly in the grid. If the user changes a property, that property on the underlying object will be affected*. This means you don't need to write any code to wire this all up. Binding can also be done from control->control so for instance you can bind another grids ItemsSource to the SelectedItems property on your first grid, and it will automatically update with which items you have selected.

Check out my answer here for more info on DataBinding

How does data binding work?

Edit:

  • I might add that any changes to the underlying object without going through the grid will still show up in the grid but only if the object implements a property changed notification mechanism eg INotifyPropertyChanged (in System.ComponentModel namespace)

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