如何在WPF C#中的datagrid中获得2行的不同索引? [英] how to get different index of 2 row in datagrid in wpf c#?

查看:58
本文介绍了如何在WPF C#中的datagrid中获得2行的不同索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天前已在以下位置发布了此问题:

i have posted this question below some days ago :

在wpf或winform中使用datagrid(或datagridview)出现问题

现在,我还有另一个问题:

now , i have another question :

我想以这种方式在datagrid中获得2行的不同索引,如下所示,但它不起作用!

i want to get different index of 2 row in datagrid in this way as shown in below but it do not work !

     var index = dgGroup.Items.IndexOf(dgGroup.SelectedItem) + 1;

    // Starting from the index found above loop through section rows untill you find blank row which can be identified by checking if "Group Name" does not have any value.

    for (int i = index; i < dgGroup.Items.Count; i++)
    {
        if (((DataRowView)dgGroup.Items[i]).Row.ItemArray[1].ToString().Trim() == string.Empty)
        {
            return;
        }
        else
        {
              int var1 = dgGroup.Items.IndexOf(dgGroup.SelectedItem);
              int var2 = dgGroup.Items.IndexOf(((DataRowView)dgGroup.Items[i]).Row.ItemArray[1].ToString().Trim() == string.Empty);

              if((var2-var1)==2)
             {
                 // Add data to one line data textbox
             }

            else 
            {
                  // Add data to more than one line data textbox
            }

        }
    }

出了什么问题?
感谢您的帮助。

what is the problem ? thanks for your help.

推荐答案

我不太确定您要做什么,但是我可以看到您的代码有问题。

I'm not really sure what you're trying to do, but I can see a problem with your code.

int var1 = dgGroup.Items.IndexOf(dgGroup.SelectedItem);

上一行将获取所选行的索引,但是下一行是错误的:

This previous line will get the index of the selected row, but the following line is wrong:

int var2 = dgGroup.Items.IndexOf(((DataRowView)dgGroup.Items[i]).Row.ItemArray[1].
ToString().Trim() == string.Empty);

我什至看不到如何编译...您正在尝试使用以下代码作为 IndexOf 方法的输入参数,该方法应从集合中获取一个项目,而不是 bool 值:

I don't see how that can even compile... you are trying to use the following code as the input parameter of the IndexOf method, which should take an item from the collection and not a bool value:

((DataRowView)dgGroup.Items[i]).Row.ItemArray[1].ToString().Trim() == string.Empty

我认为您需要用该行替换:

I think that you need to replace that line with this instead:

int var2 = dgGroup.Items.IndexOf(((DataRowView)dgGroup.Items[i]).Row);

此外,如果这些代码不应该包含在语句而不是 else ?您说过要查找组名称为空白的项目,但是在您的 if 语句中,找到空白条目后就返回。

Also, shouldn't these bits of code be in the if statement instead of the else? You said you want to find the item with the blank group name, but in your if statement, you just return when you find the blank entry.

这篇关于如何在WPF C#中的datagrid中获得2行的不同索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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