我如何解决选定的一行问题? [英] How I can solve selected one row problem?

查看:49
本文介绍了我如何解决选定的一行问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我遇到了这个小问题。

我需要的是:



从datagrid复制一行。复制多行很好,效果很好。只有当我想要选择一行时,它才行不通。



这是代码:



Hello, i got this little problem.
What i need is:

Copy one row from datagrid. Copying multiple rows is fine and works very well. Only when i want select one row, it doesn´t work.

This is the code:

<pre lang="c#">
/// <summary>
        /// Method which will copy entire row.
        /// </summary>
        private void CopyRow(object obj)
        {
            var datagrid = obj as System.Windows.Controls.DataGrid;

            var valsCollection = new List<string>();

            foreach (ENotifiedTruck item in datagrid.SelectedItems)
            {
                valsCollection.Add(item.ToStringLP());
            }

            var rows = GetDataGridRows(datagrid);            
            int i1 = 1;
            bool selectedRows = false;

            foreach (DataGridRow r in rows)
            {
                DataGridColumn column = datagrid.Columns[0];
                TextBlock cellcontent = column.GetCellContent(r) as TextBlock;
                
                valsCollection[i1] = string.Format("{0}\t{1}", cellcontent.Text, valsCollection[i1]);
                i1++;            
            }
            
            
            datagrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
            valsCollection.Insert(0, string.Empty);
            ApplicationCommands.Copy.Execute(null, datagrid);
            string oldresult = (string)Clipboard.GetData(DataFormats.Text);

            List<string> rowCollection = new List<string>();
            rowCollection = oldresult.Split(new char[] { '\n' }).ToList<string>();

            if (rowCollection.Count == 0)
                return;

            string last = rowCollection[0];
            rowCollection = new List<string>();
            rowCollection.Add(last);
            rowCollection.AddRange(valsCollection);          

            oldresult = string.Join("\n", rowCollection);
            Clipboard.SetText($"{oldresult}");
        }


        public IEnumerable<DataGridRow> GetDataGridRows(System.Windows.Controls.DataGrid grid)
        {
            var itemsSource = grid.ItemsSource as IEnumerable;
            if (null == itemsSource) yield return null;
            foreach (var item in itemsSource)
            {
                var rows =  (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
                if (null != rows) yield return rows;                           
            }           
        }







对我来说,当我选择一行时会出现问题。在这一特定行:

我收到错误:




For me problem happen when i select one row. In this particular line:
I get error:

valsCollection[i1] = string.Format("{0}\t{1}", cellcontent.Text, valsCollection[i1]);





错误是:



The error is: "

"{Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}





任意想法如何解决?谢谢!



我的尝试:



我试过做它通过selectedIndex,但它不起作用。

"

Any ideas how to solve it? Thanks!

What I have tried:

I tried to do it via selectedIndex, but it didn´t work.

推荐答案

{oldresult});
}


public IEnumerable< DataGridRow> GetDataGridRows(System.Windows.Controls.DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if(null == itemsSource)yield return null;
foreach(itemsSource中的var项)
{
var rows =(DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
if(null!= rows)产生返回行;
}
}
"{oldresult}"); } public IEnumerable<DataGridRow> GetDataGridRows(System.Windows.Controls.DataGrid grid) { var itemsSource = grid.ItemsSource as IEnumerable; if (null == itemsSource) yield return null; foreach (var item in itemsSource) { var rows = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item); if (null != rows) yield return rows; } }







对我来说,当我选择一行时会出现问题。在这一特定行:

我收到错误:




For me problem happen when i select one row. In this particular line:
I get error:

valsCollection[i1] = string.Format("{0}\t{1}", cellcontent.Text, valsCollection[i1]);





错误是:



The error is: "

"{Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}





任意想法如何解决?谢谢!



我的尝试:



我试过做它通过selectedIndex,但它不起作用。

"

Any ideas how to solve it? Thanks!

What I have tried:

I tried to do it via selectedIndex, but it didn´t work.


与.NET中的所有集合一样,数组索引从零开始,而不是一个:

Like all collections in .NET, array indexes start at zero, not one:
int i1 = 1;
...
foreach (DataGridRow r in rows)
{
    ...
    valsCollection[i1] = string.Format("{0}\t{1}", cellcontent.Text, valsCollection[i1]);


这篇关于我如何解决选定的一行问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆