c#代码避免网格视图中的重复行
本文关键字:视图 代码 网格 | 更新日期: 2023-09-27 18:13:38
为了防止在某些字段的基础上重复行,我编写了以下代码。
我在网格视图中使用模板字段,如txtlicensennumber和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;
}
}
}
}
}
请注意,Grid具有以下字段和行值。
[lblJurisdiction] - [txtlicensennumber] - [IssueDate]
[Abc ] - [ 123年 ] - [ 12/12/2015]
[Abc ] - [ 123年 ] - [ 12/12/2015]
[Abc ] - [ 123年 ] - [ 12/12/2015]
[def] - [123] - [12/12/2015]
(def ] - [ 123年 ] - [ 12/12/2015]
(def ] - [ 123年 ] - [ 12/12/2015]
由于数据绑定时的连接操作,这些值在网格视图中重复。由于某些要求,我只能取一个
现在我只想让其中一个可见,比如
[Abc ] - [ 123年 ] - [ 12/12/2015]
(def ] - [ 123年 ] - [ 12/12/2015]
上面的代码不起作用。请帮忙!!
您的内循环有问题。它应该从(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;
}
}
}
}
}
使用DefaultView的更好方法。从datatable(gridview的来源)中删除重复的行。
datatable = datatable.DefaultView.ToTable(true, "Col1ToCompare", "Col2ToCompare");
我使用了一种方法来合并具有相似值的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--;
}
}
最后把上面的方法放到页面加载中,看看结果。希望有帮助。