2015-12-22 48 views
1

编写所需的单词显示在文本框中。我想有多行...但是,通过此代码,我只得到一行选择.. ,我想输出到屏幕行..而不是绘画,我想只显示指定的行(匹配文本框的文本)。如何实现这一目标?C#如何更改我的代码?

 try 
     { 

      dataGridView1.ClearSelection(); //or restore rows backcolor to default 
      for (int i = 0; i < (dataGridView1.Rows.Count); i++) 
      { 
       for (int j = 0; j < (dataGridView1.Columns.Count); j++) 
        if (dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture)) 
         //(dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture)) 
        { 
         dataGridView1.FirstDisplayedScrollingRowIndex = i; 
         dataGridView1.Rows[i].Selected = true; //It is also possible to color the row backgroud 
         return; 
        } 
      } 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("not exist"); 
     } 
+0

你只想过滤网格的数据源? – Sachin

+0

也许......?对不起,我不好英语..我做了,使用datagridview..load csv文件..不使用数据表..所以,然后我想过滤搜索功能。 – Simkyujin

+0

Windows桌面应用程序? – Sachin

回答

0

如果要筛选基础上,搜索框,那么你应该把你的数据到datatable数据和过滤dataview并绑定该dataviewdatagridview。 (你可以学习,在herehere

但是,如果你想在当前代码改变,那么你应该删除return,因为它会从该行返回,将不匹配其他行

 try 
     { 

      dataGridView1.ClearSelection(); //or restore rows backcolor to default 
      for (int i = 0; i < (dataGridView1.Rows.Count); i++) 
      { 
       for (int j = 0; j < (dataGridView1.Columns.Count); j++) 
        if ((dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture)) == false) 
         //(dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture)) 
        { 
         //dataGridView1.FirstDisplayedScrollingRowIndex = i; 
         //dataGridView1.Rows[i].Selected = true; //It is also possible to color the row backgroud 
         //return; 
         dataGridView1.Rows.Remove(dataGridView1.Rows[i]); 
        } 
      } 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("not exist"); 
     } 
+0

哦,好吧,我明白了。嗯...一个更多的问题..我不想数据表..因为我不能使用那..我是初学者..我想在文本框中查看通过屏幕只搜索单元格。有没有办法? – Simkyujin

+1

这段代码也适用于它,因为它只是在datagrid的行中搜索..但请记住,如果您搜索了“hello”,那么它将显示以“hello”开头的行并删除其他所有&you不能找回他们,直到你从CSV重新绑定数据网格。并且数据表对于初学者也很容易..你应该试试.. – Sachin

+0

我可以知道为什么downvoted? – Sachin