0

使用 MS Visual Studio 和 C#.net 4.0。

所以,刚刚完成了我的程序的另一部分,它检查重复项,最终“非常感谢所有帮助”。向我的老板展示了喜欢它的人,然后问“他是否要选择显示具有重复值的零件号的 datagridview 的结果,是否可以突出显示与所选结果相等的 maindatagridview”。

现在首先我明白他的意思,但措辞相当困难,因此寻找一些例子让我开始非常困难。

现在虽然我没有任何代码,但我可以显示我目前拥有的代码。

我做的第一件事是在数据网格上识别一个事件处理程序,它可以检测选择了哪一行,我将使用“selectionchanged”。

更新:: 好的,虽然我会告诉你我重用我的代码的意思。请注意,代码非常相似,但只是一个起点,我可能会将以前的方法合并到新的方法中。

private void MyErrorGrid_SelectionChanged(object sender, EventArgs e)
    {

        string getPartSelected;
        getPartSelected = MyErrorGrid.CurrentCell.Value.ToString();


        foreach (DataGridViewRow row in ParetoGrid.Rows)
        {
            var cellValue = row.Cells["Keycode"].Value;
            if (cellValue != null && cellValue.ToString() == getPartSelected)
            {
                ParetoGrid.Rows[row.Index].DefaultCellStyle.BackColor = Color.Red;
            }
        }

    }

如您所见,这是可行的,但是存在一些问题。它突出显示但没有取消突出显示,所以我想我需要存储以前选择的?(不确定这是最好的方法)。

还需要添加导航,因为突出显示对用户来说不够好。At the moment ive added in selected = true but again when the selection changes i need to use the previous value.

4

1 回答 1

0

好的,这感觉就像我在作弊,但这就是我为解决问题所做的。

private void MyErrorGrid_SelectionChanged(object sender, EventArgs e)
    {

        string getPartSelected;

        getPartSelected = MyErrorGrid.CurrentCell.Value.ToString();

        foreach(DataGridViewRow allrow in ParetoGrid.Rows)
        {
            ParetoGrid.Rows[allrow.Index].DefaultCellStyle.BackColor = Color.Empty;
            ParetoGrid.Rows[allrow.Index].Selected = false;
        }
        //might need a do while
        foreach (DataGridViewRow row in ParetoGrid.Rows)
        {
            var cellValue = row.Cells["Keycode"].Value;

            if (cellValue != null && cellValue.ToString() == getPartSelected)
            {
                ParetoGrid.Rows[row.Index].DefaultCellStyle.BackColor = Color.Red;
                ParetoGrid.Rows[row.Index].Selected = true;


            }
        }

    }

正如您所看到的,它在进入下一个更改之前基本上清除了所有内容,因此它可以工作但不使用以前的值。如果有人有更好的解决方案,请随时回答

唯一的事情是当有很多行时,虽然它选择并突出显示它不滚动 datagridview 是否有类似的方法可以做到这一点?

于 2012-04-25T10:17:22.220 回答