2016-10-19 56 views
2

いくつかのフィールドに基づいて重複行を避けるために、次のコードを記述しました。グリッドビューの重複行を避けるためにC#コード

私はtxtLicenseNumberとlblJurisdictionのようなグリッドビューでテンプレートフィールドを使用しています。

private void AvoidDuplicate() 
    { 

     for (int i = 0; i < grdView.Rows.Count; i++) 
     { 
      TextBox txtoldvalue = grdView.Rows[i].FindControl("txtLicenseNumber") as TextBox; 
      string oldvalue = txtoldvalue.Text.ToString(); 

      Label txtoldvalueJ = grdView.Rows[i].FindControl("lblJurisdiction") as Label; 
      string oldvalueJ = txtoldvalueJ.Text.ToString(); 

      if (oldvalue != "" || oldvalueJ !="") 
      { 
       for (int j = 0; j < i; j++) 
       { 
        TextBox txtnewvalue = grdView.Rows[j].FindControl("txtLicenseNumber") as TextBox; 
        string newvalue = txtnewvalue.Text.ToString(); 

        Label txtnewvalueJ = grdView.Rows[j].FindControl("lblJurisdiction") as Label; 
        string newvalueJ = txtnewvalueJ.Text.ToString(); 

         if (oldvalue != newvalue && oldvalueJ != newvalueJ) 
         { 
          grdView.Rows[i].Visible = true; 
         } 
         else 
         { 
          grdView.Rows[i].Visible = false; 
         } 
        } 
      } 
     } 
    } 

グリッドには、次のフィールドと行の値があります。

  • [lblJurisdiction] - [txtLicenseNumber] - [IssueDate]

  • [Abcの] - [123] - [2015年12月12日]

  • [Abcの] - [123] - 【2015年12月12日]

  • [Abcの] - [123] - [2015年12月12日]

  • [DEF] - [123] - [2015年12月12日]

  • [DEF] - [123] - [2015年12月12日]

  • [DEF] - [123] - [2015年12月12日]

値が重複していますデータバインディング時の結合操作のためにグリッドビューに表示されます。いくつかの要件のために、私は1つだけを取得する必要があります。

今、私は

  • のようなそれらの一方のみが見えるようにしたい[Abcの] - [123] - [2015年12月12日]

  • [DEF] - [123] - [12/12/2015]

上記のコードは機能しません。助けてください !!!

+0

単純なLinqを使用して、Databoundである基になるコレクションをフィルタリングしないと、それははるかに簡単です。私はGridViewがDataTableであると仮定しています –

+0

あなたはタスクを達成するために簡単なLinq Distinctと 'IEqualityComparer 'が必要です。コードの3-4行だけです。 –

答えて

1

内部ループに問題があります。あなたの行数に(i + 1)で始まる必要があります。内部ループと他のループが同じ行をフェッチしていました。 [既定を使用する

private void AvoidDuplicate() 
    { 

     for (int i = 0; i < grdView.Rows.Count; i++) 
     { 
      TextBox txtoldvalue = grdView.Rows[i].FindControl("txtLicenseNumber") as TextBox; 
      string oldvalue = txtoldvalue.Text.ToString(); 

      Label txtoldvalueJ = grdView.Rows[i].FindControl("lblJurisdiction") as Label; 
      string oldvalueJ = txtoldvalueJ.Text.ToString(); 

      if (oldvalue != "" || oldvalueJ !="") 
      { 
       for (int j = i+1; j < grdView.Rows.Count; j++) 
       { 
        TextBox txtnewvalue = grdView.Rows[j].FindControl("txtLicenseNumber") as TextBox; 
        string newvalue = txtnewvalue.Text.ToString(); 

        Label txtnewvalueJ = grdView.Rows[j].FindControl("lblJurisdiction") as Label; 
        string newvalueJ = txtnewvalueJ.Text.ToString(); 

         if (oldvalue != newvalue || oldvalueJ != newvalueJ) 
         { 
          grdView.Rows[i].Visible = true; 
         } 
         else 
         { 
          grdView.Rows[i].Visible = false; 
          break; 
         } 
        } 
      } 
     } 
    } 

良い方法。重複する行をデータテーブル(グリッドビューのソース)から削除します。

datatable = datatable.DefaultView.ToTable(true, "Col1ToCompare", "Col2ToCompare"); 
+0

上記のコードでは1つしか取得できません。 – Nida

+0

Hmm。 valueとvalueJのANDをORに変更する必要があります。答えを更新しました。 –

+0

もう一度申し訳ありません...今はAbcを2回取得して1回defを取得しています... – Nida

1

類似の値を持つGridView行をマージする方法を使用しました。気にしないで。あなたのコードを編集したりレビューしたりするつもりはありません。しかし、私が使用したものを次のように共有してください:

private void AvoidDuplicates() 
{ 
    int i = GridView1.Rows.Count - 2; //GridView row count 
    while (i >= 0) //Iterates through a while loop to get row index 
    { 
     GridViewRow curRow = GridView1.Rows[i]; //Gets the current row 
     GridViewRow preRow = GridView1.Rows[i + 1]; //Gets the previous row 

     int j = 0; 
     while (j < curRow.Cells.Count) //Inner loop to get the row values 
     { 
      /****Condition to check if it has duplicate rows - Starts****/ 
      if (curRow.Cells[j].Text == preRow.Cells[j].Text) //Matches the row values 
      { 
       if (preRow.Cells[j].RowSpan < 2) 
       { 
        curRow.Cells[j].RowSpan = 2; 
        preRow.Cells[j].Visible = false; 
       } 
       else 
       { 
        curRow.Cells[j].RowSpan = preRow.Cells[j].RowSpan + 1; 
        preRow.Cells[j].Visible = false; 
       } 
      } 
      /****Ccondition to check if it has duplicate rows - Ends****/ 
      j++; 
     } 
     i--; 
    } 
} 

最後に上記の方法をページの読み込みに置き、結果を確認してください。希望は助けます。

関連する問題