【问题标题】:Search value in textBox so it finds it in the data located in gridView在 textBox 中搜索值,以便在位于 gridView 的数据中找到它
【发布时间】:2016-03-25 13:46:58
【问题描述】:

目前我正在打开一个 .xls 文件,方法是在文本框中输入文件位置,我想通过在文本框中输入某个值来搜索某个值并删除该值所在的行。

这是 Form1 的代码,其中包含 dataGridView:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Plan_de_lucru_1._0
{
    public partial class frPlanMain : Form
    {
        public frPlanMain()
        {
            InitializeComponent();
        }

        private void frPlanMain_Load(object sender, EventArgs e)
        {

        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            string constr = "Provider = MicroSoft.Jet.OLEDB.4.0; Data Source=" + locTBox.Text + "; Extended Properties =\"Excel 8.0; HDR=Yes;\";";
            OleDbConnection con = new OleDbConnection(constr);
            OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + shTBox.Text + "$]", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dGVPlan.DataSource = dt;
            new SearchWindow().Show();
            this.Show();
        }
    }
}

这是 Form2,其中包含我要在其中输入搜索值的文本框和按钮。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plan_de_lucru_1._0
{
    public partial class SearchWindow : Form
    {
        public SearchWindow()
        {
            InitializeComponent();
        }

        private void SearchButtonW_Click(object sender, EventArgs e)
        {
            String str = "select * from searchBox where ( Name like '%' + @search + '%')";
            BindingSource bs = new BindingSource();

        }
    }
}

感谢您的帮助。

【问题讨论】:

  • 您可以使用最初绑定到现有数据网格视图的 DataTable 来执行此操作。 DataTable 有一个 Filter 属性我将发布一个简单的例子来说明这是多么容易..
  • 我认为这里的其中一个选项可以为您解决问题! stackoverflow.com/questions/31809201/…

标签: c# asp.net gridview datagridview


【解决方案1】:

我有一个在类级别声明的 DataTable。

protected static DataTable dtSearch = null; 

然后我有一个按钮点击搜索 DataTable 以根据我在 Textbox.Text 字段中键入的值进行过滤。

protected void btnSrch_Click(object sender, EventArgs e)
{
    if (txtSearch.Text.Length > 0)
    {
        dtSearch = dtInitialDatable;//the datatable that was bound to my DatagridView 
        dv = new DataView(dtSearch);
        dv.RowFilter = "Data_Column_Name LIKE '%" + txtSearch.Text.ToUpper() + "%'";
        //bind the DataGridView to dv 
        myDataGridView.DataSource = dv;
        //myDataGridView.DataBind();//uncomment if you are doing this in webform
    }
    else
    {
        dtSearch = null;
        dv = null;
    }
}

【讨论】:

    【解决方案2】:

    这对我有用:

    namespace Plan_de_lucru_1._0
    {
        public partial class SearchWindow : Form
        {
            public frPlanMain refTofrPlanMain;
    
            public SearchWindow(frPlanMain f) //<<Edit made here 
            {
                refTofrPlanMain = f;
                InitializeComponent();
            }
    
            private void SearchButtonW_Click(object sender, EventArgs e)
            {
                {
                    (refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
                    foreach (DataGridViewRow item in refTofrPlanMain.dGVPlan.Rows)
                    {
                        if (item.Visible)
                        {
                            item.Selected = true;
                            break;
    

    【讨论】:

      猜你喜欢
      • 2016-07-17
      • 1970-01-01
      • 2015-11-17
      • 2017-09-23
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多