【问题标题】:How to show only filtered data如何仅显示过滤后的数据
【发布时间】:2014-09-07 04:34:11
【问题描述】:

大家早上好。我想在我的表中只显示过滤记录,这怎么可能我当前的代码显示突出显示的过滤记录和其他非过滤记录。这怎么可能。

private void textBox2_TextChanged(object sender, EventArgs e)
   {
       try
       {
           this.dataGridView1.ClearSelection();
           foreach (DataGridViewRow r in this.dataGridView1.Rows)
           {
               if (r.Cells[1].Value != null)
               {
                   if ((r.Cells[1].Value).ToString().StartsWith(this.textBox2.Text.Trim()))
                   {
                       this.dataGridView1.Rows[r.Index].Selected = true;
                   }
               }
           }
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.ToString());
        }
    } 
private void dataGridView1_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.ClearSelection();
    }    

【问题讨论】:

  • 你的“过滤”只设置Selected = true。听起来你想要更像Visible = true/false
  • 我只想显示过滤后的记录。目前它显示所有记录并突出显示过滤记录。

标签: c# sql datagridview filter


【解决方案1】:

Yorye Nathan 的评论是正确的

try
            {
                foreach (DataGridViewRow r in this.dataGridView1.Rows)
                {
                    if (r.Cells[0].Value != null)
                    {
                        if ((r.Cells[0].Value).ToString().StartsWith(this.textBox1.Text.Trim()) && textBox1.Text.Trim().Length > 0)
                        { 
                            this.dataGridView1.Rows[r.Index].Visible = false;
                        }
                        else
                        {
                            this.dataGridView1.Rows[r.Index].Visible = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

假设输入类似于

this.dataGridView1.Columns.Add("Text", "Text");
            this.dataGridView1.Rows.Add("five");
            this.dataGridView1.Rows.Add("six");
            this.dataGridView1.Rows.Add("seven");
            this.dataGridView1.Rows.Add("eight");
            this.dataGridView1.Rows.Add("nine");

【讨论】:

  • 此代码无效。实际上,我已经在前端使用 datagridview 任务将 datagridview 与数据库绑定了。
  • @user3471218 过滤绑定前的结果
  • 感谢您的回复。实际上,我的 datagridview 需要手动绑定,因为某些任务无法通过编码进行绑定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 2013-02-25
  • 1970-01-01
  • 2021-12-19
  • 2022-11-23
  • 2017-04-16
相关资源
最近更新 更多