【问题标题】:Filtering a datagridview that is bound to a dataset with a textbox c#使用文本框 c# 过滤绑定到数据集的 datagridview
【发布时间】:2016-12-16 17:48:12
【问题描述】:

我正在尝试过滤已绑定到数据集的 datagridview。问题是,当我在文本框中输入数据时,应用程序停止并且错误是我尝试过滤的列不存在。我尝试使用字符串查询填充 datagridview 并且过滤工作正常,但我无法更新 datagridview(我不知道为什么)。这就是为什么我用数据集而不是查询来填充它。有什么建议么?这就是我所拥有的:

DataTable dt = new DataTable("Items");
private void LoadDataGrid()
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT Items.ItemID AS #, Items.SerialNo AS 'SERIALNO', Items.Description AS DESCRIPTION, Items.MaxVoltage, Items.FrameSize, Items.ArrivalDate, Items.DepartureDate, Items.Notes, Items.MechType, Items.[Fix-Drawout], CONCAT(Location.Rack, Location.Row, Location.Columnn, Location.Position) AS LOCATION, ItemStatus.Description AS STATUS, Type.Description AS TYPE, Manufacturers.Description AS MANUFACTURERS FROM Items INNER JOIN Location ON Items.LocationID = Location.LocationID INNER JOIN ItemStatus ON Items.Status = ItemStatus.StatusID INNER JOIN Type ON Items.TypeID = Type.TypeID INNER JOIN Manufacturers ON Items.ManufacturerID = Manufacturers.ManufacturerID", AEAcnn))
{
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}

这是我正在使用的过滤器表达式(带有组合框)

private void txtFilter_KeyPress(object sender, KeyPressEventArgs e)
{
if (cmbFilterSearch.Text == "TYPE")
{
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("TYPE LIKE '%{0}%'", textBox14.Text);
dataGridView1.DataSource = dv.ToTable();
}
}

当找不到列 TYPE 时,应用程序崩溃,即使名称实际上是 TYPE。

【问题讨论】:

  • 如果您包含您的代码尝试供人们审查,这个问题会更好。
  • 我的错!这就是我所拥有的。
  • 您没有显示您正在使用的过滤器表达式
  • 对!就是这样。
  • 在类型周围加上括号。 "[TYPE] LIKE '%{0}%'"

标签: c# sql winforms


【解决方案1】:

试试这样的:

        if (dt.Rows.Count > 0)
        {
            string str = string.Format("[TYPE] LIKE '%{0}%'", textBox14.Text.Replace("'", "''"));
            dt.CaseSensitive = false;
            DataTable dt1 = dt.Select(str).CopyToDataTable();
            dataGridView1.DataSource = dt1;
        }

【讨论】:

  • 好吧,应用程序不会因此而崩溃,但现在它不会过滤任何东西。
  • 但是我的搜索需要使用组合框 (cmbFilterSearch.Text == "TYPE") 进行过滤,并且您没有在答案中包含它,我这样做了,应用程序再次崩溃
  • 组合框有时很敏感。取决于它们是如何填充的。尝试将Text 替换为SelectedItemSelectedTextSelectedValue,而不是cmbFilterSearch.Text ==。我对组合框也有类似的问题。尝试所有 3 个,看看哪一个有效。
  • 我尝试了所有三个,但没有任何效果。这个项目讨厌我。大声笑
【解决方案2】:

尽管列名非常规,但如果您过去使用适配器填充数据表,我看不出代码有任何不正确之处,这显然是这种情况,因为在按键事件之前您没有收到任何错误。由于我们看不到代码的整个上下文,我怀疑您没有绑定您认为的数据,或者绑定正在更改您没有注意到的其他地方。 请参阅this LINQPad script 以证明其有效:

Form frm = new Form();
DataTable dt = new DataTable("Items");
DataGridView dataGridView1 = new DataGridView();
TextBox textBox14 = new TextBox();
TextBox cmbFilterSearch = new TextBox();

void Main()
{   
    PopulateDataTable(dt);  
    BuildForm(frm, dt);
    Application.Run(frm);
}

void BuildForm(Form frm, DataTable dt)
{
    frm.Height = 500;
    frm.Width = 900;

    cmbFilterSearch.Text = "TYPE";
    frm.Controls.Add(cmbFilterSearch);

    textBox14.Text = "filter...";
    textBox14.Location = new Point(0,50);
    frm.Controls.Add(textBox14);
    textBox14.KeyPress += txtFilter_KeyPress;

    dataGridView1.DataSource = dt;
    dataGridView1.Location = new Point(0, 80);
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.Width = 800;  
    frm.Controls.Add(dataGridView1);
}

void txtFilter_KeyPress(object sender, KeyPressEventArgs e)
{
    if (cmbFilterSearch.Text == "TYPE")
    {
        DataView dv = dt.DefaultView;
        dv.RowFilter = string.Format("TYPE LIKE '%{0}%'", textBox14.Text);
        Console.WriteLine(dv.RowFilter);
        dataGridView1.DataSource = dv.ToTable();
    }
}

void PopulateDataTable(DataTable dt)
{
    dt.Columns.Add("#", typeof(int));
    dt.Columns.Add("'SERIALNO'", typeof(string));
    dt.Columns.Add("DESCRIPTION", typeof(string));
    dt.Columns.Add("MaxVoltage", typeof(string));
    dt.Columns.Add("FrameSize", typeof(string));
    dt.Columns.Add("ArrivalDate", typeof(DateTime));
    dt.Columns.Add("DepartureDate", typeof(DateTime));
    dt.Columns.Add("Notes", typeof(string));
    dt.Columns.Add("MechType", typeof(string));
    dt.Columns.Add("Fix-Drawout", typeof(string));
    dt.Columns.Add("LOCATION", typeof(string));
    dt.Columns.Add("STATUS", typeof(string));
    dt.Columns.Add("TYPE", typeof(string));
    dt.Columns.Add("MANUFACTURERS", typeof(string));

    DataRow row = dt.NewRow();
    row[0] = 1;
    row[1] = "9083290823";
    row[2] = "Mares eat oats";
    row[3] = "12v";
    row[4] = "60";
    row[5] = DateTime.Now;
    row[6] = DateTime.Now.AddDays(7);
    row[7] = "and does eat oats";
    row[8] = "and little lambs";
    row[9] = "eat ivy.";
    row[10] = "Cancelled";
    row[11] = "KEYWORD";
    row[12] = "ACME";
    dt.Rows.Add(row);

    row = dt.NewRow();
    row[0] = 2;
    row[1] = "43537953";
    row[2] = "Mares eat oats";
    row[3] = "12v";
    row[4] = "60";
    row[5] = DateTime.Now;
    row[6] = DateTime.Now.AddDays(7);
    row[7] = "and does eat oats";
    row[8] = "and little lambs";
    row[9] = "eat ivy.";
    row[10] = "Cancelled";
    row[11] = "Reserved Word";
    row[12] = "Conglomico";
    dt.Rows.Add(row);

    row = dt.NewRow();
    row[0] = 3;
    row[1] = "9083290823";
    row[2] = "Mares eat oats";
    row[3] = "12v";
    row[4] = "60";
    row[5] = DateTime.Now;
    row[6] = DateTime.Now.AddDays(7);
    row[7] = "and does eat oats";
    row[8] = "and little lambs";
    row[9] = "eat ivy.";
    row[10] = "Cancelled";
    row[11] = "Identifier";
    row[12] = "Enormico";
    dt.Rows.Add(row);

}

【讨论】:

    【解决方案3】:

    试试这个方法。

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string connetionString = null;
                SqlConnection connection ;
                SqlCommand command ;
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                DataView dv ;
                string sql = null;
                connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
                sql = "Select * from product";
                connection = new SqlConnection(connetionString);
                try
                {
                    connection.Open();
                    command = new SqlCommand(sql, connection);
                    adapter.SelectCommand = command;
                    adapter.Fill(ds, "Filter DataView");
                    adapter.Dispose();
                    command.Dispose();
                    connection.Close();
    
                    dv = new DataView(ds.Tables[0], "Product_Price < = 3000", "Product_Name", DataViewRowState.CurrentRows);
    
                    dataGridView1.DataSource = dv;
                }
                catch (Exception ex)
                {
                    MessageBox.Show (ex.ToString());
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-10
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 2022-08-02
      • 1970-01-01
      • 2010-11-18
      相关资源
      最近更新 更多