1

ms Visual Studiocsharp .net4を使用します。

これは、重複をチェックする必要があるコードです

    public void CheckForDuplicate()
    {
        DataGridViewRowCollection coll = ParetoGrid.Rows;
        DataGridViewRowCollection colls = ParetoGrid.Rows;
        List<string> listParts = new List<string>();
        int count = 0;
        foreach (DataGridViewRow item in coll)//379
        {
            foreach (DataGridViewRow items in colls)//143641
            {
                if (items.Cells[5].Value == item.Cells[5].Value)  
                {
                    if (items.Cells[2].Value != item.Cells[2].Value)
                    {
                        listParts.Add(items.Cells["Keycode"].Value.ToString());
                        count++;
                        dupi = true;

                        //txtDupe.Text = items.Cells["Keycode"].Value.ToString();
                        //this.Refresh();
                    }
                }
            }
        }
        MyErrorGrid.DataSource = listParts;
    }

これは、ユーザーが保存できるようにする前のチェックです。

private void butSave_Click(object sender, EventArgs e)
    {
        CheckForDuplicate();
        if (dupi == true)
        {
            txtDupe.Clear();

            dupi = false;
        }
        else
        {
            SaveMyWorkI();
            dupi = false;
        }
    }

これは、それが見ているデータです: ここに画像の説明を入力

これで、保存に関係なくロジックに欠陥があるに違いないことがわかりました。私は基本的にpareto1の各セルを検索して、ユーザーが重複を作成したかどうかを確認しています。重複している場合は保存せず、代わりに部品番号などを別のデータグリッドビューに表示します....それが計画です。

誰かがこれを見て教えてくれませんか

1)私のロジックのどこでこれが失敗していますか?また、チェックが正しいかどうかはどうですか?

2)データグリッド ビューへの単純なバインドで結果を表示するのに十分な場合、リストは情報を追加して機能しますか?

3)これが本当に悪い検索方法である場合、誰かが私が達成しようとしていることを反映したコードを提供できます。

今後ともコメントをよろしくお願いいたします。

更新:: 助けてくれてありがとう、私のアルゴリズムは動作するようになりましたが、私の最後の問題は、パレート列に複製された部品番号を表示することであり、代わりに長さを表示します。

public void CheckForDuplicate()
    {
        DataGridViewRowCollection coll = ParetoGrid.Rows;
        DataGridViewRowCollection colls = ParetoGrid.Rows;
        List<string> listParts = new List<string>();
        int count = 0;
        foreach (DataGridViewRow item in coll)//379
        { 
            foreach (DataGridViewRow items in colls)//143641
            {
                count++;
                if ((items.Cells[5].Value != null))
                {
                    if ((items.Cells[5].Value != null) && (items.Cells[5].Value.Equals(item.Cells[5].Value)))
                    {
                        if ((items.Cells[2].Value != null) && !(items.Cells[2].Value.Equals(item.Cells[2].Value)))
                        {
                            listParts.Add(items.Cells["Keycode"].Value.ToString());

                            dupi = true;
                        }
                    }
                }
            }
        }
        MyErrorGrid.DataSource = listParts;
        var message = string.Join(Environment.NewLine, listParts);
        //MyErrorGrid.DataSource = message;
        MessageBox.Show(message);

    }

メッセージ ボックスには結果が正しく表示されますが、データグリッドにバインドするときに見逃しているものはありますか?

4

3 に答える 3

3

データ入力中に検証を実行する方法を示す簡単な例を次に示します。エラーの表示方法をカスタマイズできるさまざまな方法があります (エラーを解決するための何らかのカスタム ダイアログを含む)。

public partial class Form1 : Form
{
    BindingSource bs;
    DataTable dt;    public Form1()
    {
        InitializeComponent();

        BindingList<BindingClass> data = new BindingList<BindingClass>
            { 
                new BindingClass{ Name = "one" }, 
                new BindingClass { Name = "two"} 
            };

        dataGridView1.DataSource = data;
        dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);

    }

    void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Index != e.RowIndex & !row.IsNewRow)
            {
                if (row.Cells[0].Value.ToString() == e.FormattedValue.ToString())
                {
                    dataGridView1.Rows[e.RowIndex].ErrorText =
                        "Duplicate value not allowed";                    

                    e.Cancel = true;
                    return;
                }
            }
        }
        dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
    }

} 

    public class BindingClass
    {
        public string Name { get; set; }
    }

}

当然、これはユーザーが何を操作したいかという要件に常に適合するとは限りませんが、別のオプションを検討するのに役立つと思いました.

于 2012-04-24T15:04:39.453 に答える
1

==および!=との比較を行っています。

items.Cells[5].Valueオブジェクトを公開します。

あなたの場合、これは参照の等価性に基づいて等価性チェックを行っている可能性が最も高いですが、これはおそらくあなたが望むものではありません。次のようなものを使用してみてくださいitems.Cells[5].Value.Equals(item.Cells[5].Value)

利用可能な最も単純な抽象化でそのような問題を解決することも検討してください。たとえば、グリッドをオブジェクトのコレクションにバインドした場合、その上にボルトで固定した UI を無視して、そのオブジェクトのコレクションに対してクリーンアップ操作を実行できます。

また、LINQ 名前空間のDistinct拡張メソッドの使用を検討し、それにIEqualityComparer*を指定して、.NET Framework で利用可能な重複を削除するための最も効率的なコードが使用されるようにすることもできます。


*) IEqualityComparerは、2 つのオブジェクトが等しいと見なす場合に 1 か所で定義できるようにする抽象化です。Distinctは、そのような比較子を指定できるオーバーロードを提供します。

于 2012-04-24T09:16:02.893 に答える
0

これがあなたのために働くことができるかどうか見てください

 var dup = dataGridView1.Rows.Cast<DataGridViewRow>().Distinct().Where(g => g.Index != 0);

インデックス 0 の行を除外します。ヘッダー行です。

于 2012-04-24T09:22:43.087 に答える