如何使用数据网格单元格的列和行索引值设置其值?

发布于 2024-12-09 12:52:07 字数 279 浏览 1 评论 0原文

如何使用单元格的列和行索引将值插入到特定的数据网格单元格中。我将行和列索引保存为整数。

我得到的索引如下。我基本上获取单元格值、列索引和行索引,并将其作为序列化 XML 发送到 java,java 将其发送回并需要将其放在同一个单元格中。

        int column = dataGrid2.CurrentCell.Column.DisplayIndex;
        int row = dataGrid2.SelectedIndex;

谢谢,

How can i insert a value into a specific datagrid cell by using the cells column and row indexs. I have the Row and Column index saved as ints.

I got the indexs as below. Im basically taking the cell value, the column index and the row index and sending as a serialized XML to java which is sending it back and needs to put it in the same cell.

        int column = dataGrid2.CurrentCell.Column.DisplayIndex;
        int row = dataGrid2.SelectedIndex;

Thanks,

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

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

发布评论

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

评论(4

预谋 2024-12-16 12:52:07

如果你使用DataGrid你可以尝试一下

DataRowView rowView = (dtGrid.Items[rows] as DataRowView); //Get RowView
rowView.BeginEdit();
rowView[col] = "Change cell here";
rowView.EndEdit();
dtGrid.Items.Refresh(); // Refresh table

If you use DataGrid you can try it

DataRowView rowView = (dtGrid.Items[rows] as DataRowView); //Get RowView
rowView.BeginEdit();
rowView[col] = "Change cell here";
rowView.EndEdit();
dtGrid.Items.Refresh(); // Refresh table
明天过后 2024-12-16 12:52:07

对于数据网格,您可以通过 Items 属性访问行。单元格是某个项目的集合。

dataGrid2.Items[row].Cells[column].Text = "text";

只要数据在当前页面生命周期期间已绑定到数据网格,此操作就有效。如果情况并非如此,那么我相信您将被困在控制中。

For a datagrid you access rows through the Items property. The cells are a collection on an item.

dataGrid2.Items[row].Cells[column].Text = "text";

This works as long as the data has been bound to the datagrid during the current page life-cycle. If this is not the case then I believe you are stuck walking the controls.

星星的轨迹 2024-12-16 12:52:07

要以编程方式更新 WPF DataGridCell,可能有多种方法...

其中一种方法是更新绑定数据项本身中的值。这样,属性更改通知将为所有订阅的视觉对象(包括 DataGridCell 本身)触发...

反射方法

 var boundItem = dataGrid2.CurrentCell.Item;

 //// If the column is datagrid text or checkbox column
 var binding = ((DataGridTextColumn)dataGrid2.CurrentCell.Column).Binding;

 var propertyName = binding.Path.Path;
 var propInfo = boundItem.GetType().GetProperty(propertyName);
 propInfo.SetValue(boundItem, yourValue, new object[] {});

对于 DataGridComboBoxColumn,您必须提取 SelectedValuePath 并使用它来代替 propertyName

其他方法包括将单元格置于编辑模式并使用 EditingElementStyle 中的某些行为更新其内容值...我发现这很麻烦。

如果您确实需要,请告诉我。

To update a WPF DataGridCell programmatically, there could be many ways...

One of the ways is to update value in the bound data item itself. This way the property change notifications will fire for all subscribed visuals including the DataGridCell itself...

Reflection approach

 var boundItem = dataGrid2.CurrentCell.Item;

 //// If the column is datagrid text or checkbox column
 var binding = ((DataGridTextColumn)dataGrid2.CurrentCell.Column).Binding;

 var propertyName = binding.Path.Path;
 var propInfo = boundItem.GetType().GetProperty(propertyName);
 propInfo.SetValue(boundItem, yourValue, new object[] {});

For DataGridComboBoxColumn you would have to extract the SelectedValuePath and use that in place of propertyName.

Otherways include putting cell in edit mode and updating its content value using some behavior in the EditingElementStyle... I find this cumbersome.

Do let me know if you really need that.

谜泪 2024-12-16 12:52:07

我使用了基于 WPF 的变体 - 它的示例执行了整行并且成功了!:

(sender as DataGrid).RowEditEnding -= DataGrid_RowEditEnding;

foreach (var textColumn in dataGrid2.Columns.OfType<DataGridTextColumn>())
            {
                var binding = textColumn.Binding as Binding;
                if (binding != null)
                {
                    var boundItem = dataGrid2.CurrentCell.Item;
                    var propertyName = binding.Path.Path;
                    var propInfo = boundItem.GetType().GetProperty(propertyName);
                    propInfo.SetValue(boundItem, NEWVALUE, new object[] { });
                }
            }

(sender as DataGrid).RowEditEnding += DataGrid_RowEditEnding;

PS:请确保使用对该列有效的值类型(可能通过 switch 语句)。

例如:
打开 propertyName 或 propInfo... propInfo.SetValue(boundItem, (type) NEWVALUE, new object[] {});

                    switch (propertyName)
                    {
                        case "ColumnName":
                            propInfo.SetValue(boundItem, ("ColumnName"'s type) NEWVALUE, new object[] { });
                            break;
                        default:
                            break;
                    }

I used a variation based upon WPF-it's example to do the whole row and it worked!:

(sender as DataGrid).RowEditEnding -= DataGrid_RowEditEnding;

foreach (var textColumn in dataGrid2.Columns.OfType<DataGridTextColumn>())
            {
                var binding = textColumn.Binding as Binding;
                if (binding != null)
                {
                    var boundItem = dataGrid2.CurrentCell.Item;
                    var propertyName = binding.Path.Path;
                    var propInfo = boundItem.GetType().GetProperty(propertyName);
                    propInfo.SetValue(boundItem, NEWVALUE, new object[] { });
                }
            }

(sender as DataGrid).RowEditEnding += DataGrid_RowEditEnding;

PS: Be sure that you use value types that are valid for the column (perhaps via a switch statement).

e.g.:
switch on propertyName or propInfo... propInfo.SetValue(boundItem, (type) NEWVALUE, new object[] {});

                    switch (propertyName)
                    {
                        case "ColumnName":
                            propInfo.SetValue(boundItem, ("ColumnName"'s type) NEWVALUE, new object[] { });
                            break;
                        default:
                            break;
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文