当用户更改该单元格的值时,更新单元格值

时间:2018-08-16 18:45:53

标签: c# wpf datagrid

当用户通过键盘输入一些文本来更改单元格值时,我想更新绑定属性值。我已经编写了显示绑定属性值和正常工作的代码。但是我想知道当用户在特定行的特定单元格中输入一些文本时如何更新单元格值。

假设用户要更改数量。在“数量”单元格中输入一些值时,如何反映这些更改以绑定属性值并更新数据网格。

注意:还指导如何获取用户新输入的值

<DataGrid  DataGridCell.Selected="DataGrid_GotFocus" SelectionUnit="Cell" 
                   SelectionMode="Single" Name="Datagrid"  AutoGenerateColumns="False" 
                   PreviewKeyDown="Datagrid_PreviewKeyDown" 
                   CurrentCellChanged="Datagrid_CurrentCellChanged" SelectionChanged="Datagrid_SelectionChanged" SelectedCellsChanged="Datagrid_SelectedCellsChanged">

            <DataGrid.Columns>

                <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 Header="Unit Price" Width="1*" Binding="{Binding UnitPrice, Mode=TwoWay}"/>
                <DataGridTextColumn Header="Sub-Total" Width="1*" Binding="{Binding SubTotal, Mode=TwoWay}"/>
            </DataGrid.Columns>
        </DataGrid>

我已经实现了curruntcellchanged事件,效果很好。这就像按Enter键一样,它们从数据库中获取值并更新绑定属性值。 现在,如果用户更改了数量,那么如何将这种更改反映给绑定属性。

 private void Datagrid_CurrentCellChanged(object sender, EventArgs e)
        {
            //MessageBox.Show("cell chnage");
            //Datagrid.BeginEdit();
            if (Datagrid.SelectedCells.Count > 0)
            {
                DataGridCellInfo cell = Datagrid.SelectedCells[0];

                var generator = Datagrid.ItemContainerGenerator;
                int columnIndex = cell.Column.DisplayIndex;
                int rowIndex = generator.IndexFromContainer(generator.ContainerFromItem(cell.Item));

                int counts = Datagrid.Items.Count - 1;
                //MessageBox.Show("total counts: " + counts.ToString() + "Selected index " + rowIndex.ToString() + " column index: " + columnIndex);

                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();
            } 

1 个答案:

答案 0 :(得分:0)

我找到了解决方法,

 private void Datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
TextBox quantity = e.EditingElement as TextBox;
                DataGridColumn dgc = e.Column;

                if (Datagrid.SelectedCells.Count > 0)
                {
                    DataGridCellInfo cell = Datagrid.SelectedCells[0];

                    var generator = Datagrid.ItemContainerGenerator;
                    int columnIndex = cell.Column.DisplayIndex;
                    int rowIndex = generator.IndexFromContainer(generator.ContainerFromItem(cell.Item));
                    ((Item)Datagrid.Items[rowIndex]).Quantity = quantity.Text.ToString();

                    MessageBox.Show(quantity.Text.ToString());
                }
}