いくつかのフィールドに基づいて重複行を避けるために、次のコードを記述しました。グリッドビューの重複行を避けるために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]
上記のコードは機能しません。助けてください !!!
単純なLinqを使用して、Databoundである基になるコレクションをフィルタリングしないと、それははるかに簡単です。私はGridViewがDataTableであると仮定しています –
あなたはタスクを達成するために簡単なLinq Distinctと 'IEqualityComparer'が必要です。コードの3-4行だけです。 –