【问题标题】:All the rows are invisible in the grid view所有行在网格视图中都是不可见的
【发布时间】:2016-09-30 09:45:34
【问题描述】:

我写了一个类似

的方法
     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中的所有行都不可见。我只想检查是否存在具有相同值的文本框,只有其中一行应该变得不可见。请帮忙

【问题讨论】:

  • 请帮忙!!!

标签: c# asp.net c#-4.0


【解决方案1】:

尝试使用字典。要获取所有唯一值,然后检查您的字典是否包含该值,如果不使其不可见

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;
        }

    }
}

【讨论】:

  • 感谢您的回答,但我想比较网格视图文本框中已经存在的值...请帮助我
  • 这就是它的作用。字典将保存文本框中的所有唯一值。如果它是唯一值,则将其添加到字典中,否则意味着该值已被遇到,因此使行可见性为 false
  • 你能告诉我如何将值分配给字典...拜托
  • checkdictionary[newvalue]="something";这很简单地将新值作为字典的键。和价值无关紧要。关键是重要的。只需执行我在上面发布的代码并检查
  • 为什么这个答案被否决了?这是对提问者问题的完全合法的回答
【解决方案2】:

试试这个

 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();

【讨论】:

  • 对不起,上面的循环实现代码不起作用...请帮助我!!!
猜你喜欢
  • 1970-01-01
  • 2011-05-19
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 2019-10-30
相关资源
最近更新 更多