c#如何通过命令从datagrid复制多行

时间:2017-11-10 06:50:57

标签: c# wpf datagrid

我的复制命令没什么问题。我通过ctrl + c键快捷键在datagrid中使用它。

这是我复制行的实际方法。

 private void CopyRow(object obj)
    {
        var item = obj as System.Windows.Controls.DataGrid;
        var currentRow = item.SelectedIndex + 1;
        var cells = item.SelectedCells;

        if (_entityEnhancedNotifiedTruck != null)
        {
            Dictionary<ENotifiedTruckFlags, string> transf = new Dictionary<ENotifiedTruckFlags, string>();
            string value = "";

            foreach (var e in _entityEnhancedNotifiedTruck.EFlags)
            {
                switch (e)
                {
                    case ENotifiedTruckFlags.CheckRecordHasBreak:
                        value = "Řidič má nařízenou přestávku";
                        break;
                    case ENotifiedTruckFlags.CheckWeighingOn:
                        value = "Řežim kontrolního vážení";
                        break;
                    case ENotifiedTruckFlags.NoTaraWeighted:
                        value = "Chybí tárové vážení";
                        break;
                    case ENotifiedTruckFlags.SPZProblem:
                        value = "Proveďte kontrolu SPZ";
                        break;
                    case ENotifiedTruckFlags.TimeProblem:
                        value = "Proveďte kontrolu ložné plochy";
                        break;
                }
                transf.Add(e, value);
            }

            var myStringBuilder = new StringBuilder();
            foreach (KeyValuePair<ENotifiedTruckFlags, string> pair in transf)
            {
                myStringBuilder.AppendFormat("{0}", pair.Value);
                myStringBuilder.Append("," + " ");
            }

            DateTime dt = _entityEnhancedNotifiedTruck.ECreationTime;
            var Date = dt.ToShortDateString();
            var Time = dt.ToLongTimeString();

            DeliveriesGroup delGroup = _loadingSiteService.GetTruckLoadingDetail(_entityEnhancedNotifiedTruck.RecordID, computerName);


            var clipboardText = currentRow + "\t" + Date + "\t" + Time + "\t" + _entityEnhancedNotifiedTruck.LicencePlate + "\t" +  _entityEnhancedNotifiedTruck.LicencePlateTrailer + "\t" + _entityEnhancedNotifiedTruck.DriverName + "\t" + _entityEnhancedNotifiedTruck.LoadingSite + "\t" + _entityEnhancedNotifiedTruck.State + "\t" + _entityEnhancedNotifiedTruck.ProcessTypeName + "\t" + value;
            Clipboard.SetText(clipboardText);

        }
    }

我的问题是,此方法只能从datagrid复制一行。当用户将鼠标拉到多行并单击ctrl + c时,并非全部。任何提示如何解决这个问题?谢谢! :)

1 个答案:

答案 0 :(得分:0)

问题是,您的方法是通过调用Clipboard.SetText(clipboardText);直接设置剪贴板文本。

您应该重写它,因此该方法将构建的clipboardText返回为string。然后,您可以为用户选择的每一行调用此方法。

在您收集了所有行中的所有clipboardText后,您可以执行以下操作:

List<string> clipboardTextsFromAllRows = new List<string>();
// Call your method and save the return values in the list
string clipboardText = string.Join(Environment.NewLine, clipboardTextsFromAllRows);
Clipboard.SetText(clipboardText);