简体   繁体   中英

In WPF is it possible to force an update to a datagrid on keyup event?

I have a datagrid with multiple rows and columns. One of the columns is a numeric value. The user can edit this column to change the value in this cell/column. This column is totaled and the number is displayed under the datagrid. I want to have this number update as soon as the user enter a number. On the KeyUp event I call a routine that unloads the datagrid to a datatable and then I read through the column and count the value in this column. However when the datagrid is unloaded the original value is still the cell value. Is there away to force the update before I unload the datagsrid?

Here is my code:

private void dtGrid_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        int intKeyValue;


        try
        {
            if (dtGrid.CurrentColumn.DisplayIndex == 1)
            {
                if (e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Tab || e.Key == Key.NumLock)
                {
                    SendKeys.SendWait("{TAB}");
                }
                else
                {
                    intKeyValue = GetNumericValue(e.Key.ToString());
                    if (e.Key == Key.LeftShift || e.Key == Key.RightShift || e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        if (intKeyValue < 0 || intKeyValue > 9)
                        {
                            System.Windows.MessageBox.Show("Only numbers are allowed.");
                            e.Handled = true;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string strMsg = "Error occured in Start Row key event. ";
            System.Windows.MessageBox.Show(strMsg + ex.Message);
            //throw new NotImplementedException();
        }
    }


    private void dtGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        e.Handled = true;
        UpdateRowSize();
    }

private void UpdateRowSize()
    {
        DataTable dtFieldSizes = new DataTable();
        int intRowSize;
        string strTotalRowSizeData;
        string[] strRowSizeInfo;


        try
        {
            intRowSize = 0;

            dtGrid.UpdateLayout();
            dtFieldSizes = ((DataView)dtGrid.ItemsSource).ToTable();

            for (int intRowCnt = 0; intRowCnt < dtFieldSizes.Rows.Count; intRowCnt++)
            {
                intRowSize += Convert.ToInt16(dtFieldSizes.Rows[intRowCnt]["Size"]);
            }

            strTotalRowSizeData = lblRowSize.Content.ToString();
            strRowSizeInfo = strTotalRowSizeData.Split(':');

            if (Convert.ToInt16(strRowSizeInfo[1]) != intRowSize)
            {
                lblRowSize.Content = strRowSizeInfo[0] + ": " + Convert.ToString(intRowSize);
            }
        }
        catch (Exception ex)
        {
            string strMsg;

            strMsg = "RefreshRowSize, error '" + ex.Message + "' has occurred.";
            System.Windows.MessageBox.Show(strMsg);
        }
    }

You can handle AutoGeneratingColumn and set UpdateSourceTrigger of the column Binding to be PropertyChanged . Assuming that this is your DataTable:

var tab = new DataTable();
tab.Columns.Add("a", typeof(double));
tab.Rows.Add(tab.NewRow());
tab.Rows[0][0] = 45; 

And this is your DataGrid and its ItemsSource:

DataGrid gr = new DataGrid();
gr.ItemsSource = tab.AsDataView();

Handle:

gr.AutoGeneratingColumn += Gr_AutoGeneratingColumn;

in which

    private void Gr_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        DataGridTextColumn col = e.Column as DataGridTextColumn;
        if (col != null)
        {
            Binding binding = new Binding("[" + col.Header.ToString() + "]");
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
            col.Binding = binding; 
        }
    }

In this setting, the value of the DataGrid updates, while you are typing in the cell.

您可以尝试使用DataGridCell的keyup / keydown事件,而不是更新值datagrid keydown / up事件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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