我正在开发电子表格类型POINT OF SALE(POS),其中将像在excel中一样输入项目,并通过向下,向左,向右箭头键在单元格之间导航。 当程序运行时,我已经在datagrid中输入了10个空项目,就像excel空表一样,这就是为什么当用户在第一个单元格中输入一些值时,项目信息会从数据库中获取并更新Datagrid。此时,程序可以正常运行10个项目。
<DataGridTextColumn Header="Code" Width="1*" Binding="{Binding Code, Mode=TwoWay}"/>
<DataGridTextColumn Header="Name" Width="1*" Binding="{Binding Name, Mode=TwoWay}"/>
<DataGridTextColumn Header="Description" Width="1*" Binding="{Binding Description, Mode=TwoWay}"/>
<DataGridTextColumn Header="Quantity" Width="1*" Binding="{Binding Quantity, Mode=TwoWay}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="TextChanged" Handler="CellValueChanged" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Unit Price" Width="1*" Binding="{Binding UnitPrice, Mode=TwoWay}"/>
<DataGridTextColumn Header="Sub-Total" Width="1*" Binding="{Binding SubTotal, Mode=TwoWay}"/>
事件:
private void Datagrid_CurrentCellChanged(object sender, EventArgs e)
{
DataGridCellInfo cell = Datagrid.SelectedCells[0];
var generator = Datagrid.ItemContainerGenerator;
int columnIndex = cell.Column.DisplayIndex;
int rowIndex = generator.IndexFromContainer(generator.ContainerFromItem(cell.Item));
if (header == "Code")
{
if (Datagrid.SelectedCells.Count > 0)
{
//int counts = Datagrid.Items.Count;
//MessageBox.Show(counts.ToString() +" index "+rowIndex);
//MessageBox.Show("total counts: " + counts.ToString() + "Selected index " + rowIndex.ToString() + " column index: " + columnIndex);
var grid = sender as DataGrid;
var selected = grid.SelectedItems;
foreach (var items in selected)
{
if (items as Item != null)
{
string code = ((Item)Datagrid.Items[rowIndex]).Code;
Database database = new Database();
database.DBOpen();
SQLiteDataReader sQLiteDataReader = database.GetWhere(Database.TABLE_ITEMS, Database.CODE_ITEMS, code);
while (sQLiteDataReader.Read())
{
string name = sQLiteDataReader.GetString(2);
string desc = sQLiteDataReader.GetString(6);
string unitPrice = sQLiteDataReader.GetInt64(8).ToString();
string quantity = "1";
//string subTotal = (double.Parse(quantity) * double.Parse(unitPrice)).ToString();
//grossAmount = grossAmount + Double.Parse(subTotal);
Item item = new Item()
{
Code = code,
Name = name,
Description = desc,
Quantity = quantity,
UnitPrice = unitPrice,
//SubTotal = subTotal
};
((Item)Datagrid.Items[rowIndex]).Code = code;
((Item)Datagrid.Items[rowIndex]).Name = name;
((Item)Datagrid.Items[rowIndex]).Description = desc;
((Item)Datagrid.Items[rowIndex]).Quantity = quantity;
((Item)Datagrid.Items[rowIndex]).UnitPrice = unitPrice;
}
database.DBClose();
}
}
}
正如您在事件代码(当前单元已更改)中所看到的那样,我正在从数据库中获取选定的单元格值及其相关信息,并使用((Item)Datagrid.Items[rowIndex]).Code = code;
等进行更新。
问题是用户如何输入第11、12个条目?
注意:我已经在“事件”部分写了一些代码,
if (rowIndex > 10)
{
var list = (IList<Item>)Datagrid.ItemsSource;
list.Add(new Item());
}
但是有例外。
Exception: Information for developers (use Text Visualizer to read this): This exception was thrown because the generator for control 'System.Windows.Controls.DataGrid Items.Count:13' with name 'Datagrid'
收到了不一致的CollectionChanged事件序列 具有Items集合的当前状态。下列 检测到差异: 累计计数12与实际计数13不同。[累计计数为(上次复位时的计数+ #Adds-#Remove自 最后一次重置)。] 在索引10:生成器的项目“ Inventory_Control.Classes.Item”与实际项目“ Inventory_Control.Classes.Item”不同。 在索引11:生成器的项目“ {NewItemPlaceholder}”与实际项目“ Inventory_Control.Classes.Item”不同。
One or more of the following sources may have raised the wrong events: System.Windows.Controls.ItemContainerGenerator System.Windows.Controls.ItemCollection System.Windows.Data.ListCollectionView System.Collections.Generic.List`1[[Inventory_Control.Classes.Item,
库存控制,版本= 1.0.0.0,文化=中性, PublicKeyToken = null]] (星标来源更可能是造成此问题的原因。)
The most common causes are (a) changing the collection or its Count without raising a corresponding event, and (b) raising an event
带有错误的索引或项目参数。
The exception's stack trace describes how the inconsistencies were detected, not how they occurred. To get a more timely exception, set
附加属性“ PresentationTraceSources.TraceLevel”位于 生成器将值设置为“高”并重新运行场景。一种方法 是要运行类似于以下命令: System.Diagnostics.PresentationTraceSources.SetTraceLevel(myItemsControl.ItemContainerGenerator, System.Diagnostics.PresentationTraceLevel.High) 从立即窗口。这会导致检测逻辑在每个CollectionChanged事件之后运行,因此会减慢检测速度。 应用。
void AddValues()
{
Datagrid.ItemsSource = null;
List<Item> list = new List<Item>();
list.Clear();
for(int i = 0; i<10; i++)
{
string number = i.ToString();
Item item = new Item()
{
};
list.Add(item);
}
Datagrid.ItemsSource = list;
}