[英]All the rows are invisible in the grid view
我寫了一個方法,如
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();
for (int j = 0; j < grdView.Rows.Count; j++)
{
TextBox txtnewvalue = grdView.Rows[j].FindControl("txtLicenseNumber") as TextBox;
string newvalue = txtnewvalue.Text.ToString();
if (oldvalue == newvalue)
{
grdView.Rows[j].Visible = false;
}
}
}
}
該函數在頁面加載時調用。 問題是它使 gridview 中的所有行都不可見。 我只想檢查是否有具有相同值的文本框,只有一行應該變得不可見。 請幫忙
嘗試使用字典。 獲取所有唯一值,然后檢查您的字典是否包含該值,如果不使其不可見
private void AvoidDuplicate()
{
Dictionary<string,string> checkdictionary=new Dictionary<string,string>();
for (int i = 0; i < grdView.Rows.Count; i++)
{
TextBox txtnewvalue = grdView.Rows[i].FindControl("txtLicenseNumber") as TextBox;
string newvalue = txtnewvalue.Text.ToString();
if(!checkdictionary.ContainsKey(newvalue))
{
checkdictionary[newvalue]="something";
}
else
{
grdView.Rows[i].Visible = false;
}
}
}
嘗試這個
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();
for (int j = 0; j < grdView.Rows.Count; j++)
{
if( j == i)
continue;
TextBox txtnewvalue = grdView.Rows[j].FindControl("txtLicenseNumber") as TextBox;
string newvalue = txtnewvalue.Text.ToString();
if (oldvalue == newvalue)
{
grdView.Rows[j].Visible = false;
}
}
}
}
更簡單的方法是,使用DataView
//populate your datatable dt. List your columns in ToTable method that you want to include in uniqueness of row.
dt = dt.DefaultView.ToTable(true, "LicenseNumber");
GridView1.DataSource = dt;
GridView1.DataBind();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.