【问题标题】:how i do create find similar excel?我如何创建找到类似的excel?
【发布时间】:2015-05-09 06:12:20
【问题描述】:

我想以类似 EXCEL 的形式创建搜索,在列表视图中查找并放入(行) 这是我的表格:

还有我的代码:

private int searchIndex = -1;
    private void button1_Click(object sender, EventArgs e)
    {
        button1.Text = "Find Next";
        try
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                searchIndex = (searchIndex + 1) % dataGridView1.Rows.Count;
                DataGridViewRow row = dataGridView1.Rows[searchIndex];
                if (row.Cells["Product"].Value == null)
                {
                    continue;
                }

                if (row.Cells["Product"].Value.ToString().Trim().StartsWith(textBox1.Text) || row.Cells["Product"].Value.ToString().Trim().Contains(textBox1.Text))
                {
                    ListViewItem lvi = new ListViewItem(row.Cells["Product"].Value.ToString()); 
                    lvi.SubItems.Add(row.Cells["Product"].Value.ToString());
                    listView1.Items.Add(lvi);
                    dataGridView1.CurrentCell = row.Cells["Product"];
                    dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[row.Index].Index;
                    row = dataGridView1.Rows[i];
                    return;
                }
            }
        }
        catch { }
    }

在 textbox1_textchanged 中:

searchIndex = -1;
        button1.Text = "Find";
        listView1.Clear();

我想在搜索结束时发送消息... 谢谢

【问题讨论】:

    标签: c# winforms find search-engine


    【解决方案1】:

    模仿Find All 功能,填充ListView 将与Find Next 功能非常相似。这是一个示例,如果它是与Find Next 分开的按钮:

    public Form1()
    {
      InitializeComponent();
      listView1.View = View.Details;
      listView1.Columns.Add("Column");
      listView1.Columns.Add("Row");
      listView1.Columns.Add("Value");
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
      button2.Text = "Find All";
      int tempIndex = -1;
      listView1.Items.Clear();
    
      for (int i = 0; i < dataGridView1.Rows.Count; i++)
      {
        tempIndex = (tempIndex + 1) % dataGridView1.Rows.Count;
        DataGridViewRow row = dataGridView1.Rows[tempIndex];
    
        if (row.Cells["Foo"].Value == null)
        {
          continue;
        }
    
        if (row.Cells["Foo"].Value.ToString().Trim().StartsWith(textBox1.Text) || row.Cells["Foo"].Value.ToString().Trim().Contains(textBox1.Text))
        {
          DataGridViewCell cell = row.Cells["Foo"];
          ListViewItem lvRow = new ListViewItem(new string[] { cell.ColumnIndex.ToString(), cell.RowIndex.ToString(), cell.Value.ToString() });
          listView1.Items.Add(lvRow);
        }
      }
    
      MessageBox.Show("Search ended."); // Or whatever message you intend to send.
    }
    

    【讨论】:

    • 嗨,我的朋友....这不起作用!?当按下按钮 ==>> 显示消息框
    • 我可以重现您的屏幕截图的唯一方法是删除 3 个 listView1.Columns.Add("column name"); 调用。否则,此代码应该可以工作,如我在答案中编辑的屏幕截图所示。你能确认你已经添加了这些列吗?
    • 谢谢...没错...我在 textChanged ==> licstView.Clear(); 中使用
    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多