如何仅显示已过滤的数据

时间:2014-09-07 04:34:11

标签: c# sql datagridview filter

伙计们,早上好。我想在我的表中只显示过滤记录如何可能我的当前代码显示突出显示的过滤记录与其他非过滤记录。怎么可能。

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

1 个答案:

答案 0 :(得分:0)

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");
相关问题