WPF DataGrid:如何在 DataGrid 中迭代以获取行和列?

发布于 2024-08-02 01:03:30 字数 537 浏览 2 评论 0原文

如何像使用 C# 中的 Forms DataGridView 一样迭代 WPF DataGrid 的行和列?

例如,如果您有 Forms DataGridView,您可以执行以下操作:

for(int i = 0; i < formsDataGrid1.Rows.Count; i++)
{
  MessageBox.Show(formsDataGrid1.Rows[i].ToString());
  for(int j = 0; j < formsDataGrid1.Columns.Count; j++)
     MessageBox.Show(formsDataGrid1.Rows[i].Cells[j].ToString());
}

感谢您的帮助!

**编辑:

我想这样做的原因是用户将使用 DataGrid 在 DataGrid 的第二列中输入某些信息。 另外,这个 DataGrid 有多行,我希望能够获取该数据并用它更新数据库。

How can you iterate in the rows and columns of a WPF DataGrid like with a Forms DataGridView in C#?

For example, if you have Forms DataGridView you can do something like this:

for(int i = 0; i < formsDataGrid1.Rows.Count; i++)
{
  MessageBox.Show(formsDataGrid1.Rows[i].ToString());
  for(int j = 0; j < formsDataGrid1.Columns.Count; j++)
     MessageBox.Show(formsDataGrid1.Rows[i].Cells[j].ToString());
}

Thank you for any help!

**Edit:

The reason I want to do this is that the DataGrid will be used by a user to enter certain informations in the second column of the DataGrid. Also, this DataGrid has multiple rows and I want to able to get that data and update a database with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日暮斜阳 2024-08-09 01:03:30

dg 是您的 XAML DataGrid x:Name

    for (int i = 0; i < dg.Items.Count; i++)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
            for (int j = 0; j < dg.Columns.Count; j++)
            {
                TextBlock cellContent = dg.Columns[j].GetCellContent(row) as TextBlock;
                Console.WriteLine(cellContent.Text);
            }
    }

dg is your XAML DataGrid x:Name

    for (int i = 0; i < dg.Items.Count; i++)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
            for (int j = 0; j < dg.Columns.Count; j++)
            {
                TextBlock cellContent = dg.Columns[j].GetCellContent(row) as TextBlock;
                Console.WriteLine(cellContent.Text);
            }
    }
吾性傲以野 2024-08-09 01:03:30

通常,您不会这样做:您访问底层数据源而不是 DataGrid 本身。 例如,假设数据源是 IEnumerable

foreach(Foo f in foos)
{
    MessageBox.Show(f.Name);
}

编辑:

您不需要显式访问网格的特定单元格:如果网格绑定到对象列表,则当用户编辑网格中相应的单元格时,对象的属性将自动更新。

带有联系人列表的简单示例:

    public class Contact
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
    ...
    ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();
    dataGrid.ItemsSource = contacts;

    ...

Typically, you don't do that : you access the underlying data source instead of the DataGrid itself. For instance, assuming the data source is an IEnumerable<Foo> :

foreach(Foo f in foos)
{
    MessageBox.Show(f.Name);
}

EDIT:

You don't need to access explicitly a specific cell of the grid : if the grid is bound to a list of objects, the object's property will be automatically updated when the user edits the corresponding cell in the grid.

Simple example with a list of contacts :

    public class Contact
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
    ...
    ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();
    dataGrid.ItemsSource = contacts;

    ...
始终不够爱げ你 2024-08-09 01:03:30

尝试使用 Grid.Column[x].GetCellContent(Row[y]) 方法

Try to use Grid.Column[x].GetCellContent(Row[y]) method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文