简体   繁体   English

如何在WPF C#中访问Datagridrow中的特定单元格

[英]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()). 我有一个数据网格,在其中使用了foreach(myDataGrid中的DataGridRow gvr),我需要能够从行中的特定单元格获取信息,并将其放入各自的类属性中,即(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... 这是我的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? 问题是-您目前将ItemsSource设置为什么?

Ideally this should be a strongly typed collection of Appointment objects ( ObservableCollection<Appointment> maybe). 理想情况下,这应该是Appointment对象的强类型集合(可能是ObservableCollection<Appointment> )。 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). 如果是这样,您需要提供一个在源集合上执行Add的按钮,或者让数据网格来执行此操作(我认为它支持“空行”,您可以在其中键入值...尽管我通常使用Telerik的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) 如果此集合不支持Add方法(例如IBindingList支持),则我认为默认值是使用该类型的无参数构造函数创建一个新项目(对此不太确定,可能值得阅读)

Basically, by binding these properties TwoWay it means that each item in the list can be edited by the user directly in the grid. 基本上,通过绑定这些属性TwoWay意味着用户可以直接在网格中编辑列表中的每个项目。 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. 绑定也可以通过control-> control完成,例如,您可以将另一个网格ItemsSource绑定到第一个网格上的SelectedItems属性,它将自动更新您选择的项目。

Check out my answer here for more info on DataBinding 在这里查看我的答案以获取有关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) 我可能要补充一点,即仅在对象实现属性更改的通知机制(例如INotifyPropertyChanged (在System.ComponentModel命名空间中))时,对未通过网格的基础对象的任何更改仍会显示在网格中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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