[英]C# Can't filter anything on GridView
I'm trying to filter some data on my gridview in a windows form.我正在尝试以 windows 形式过滤我的 gridview 上的一些数据。 My database connection is good;
我的数据库连接良好; I receive all data for the gridview.
我收到了 gridview 的所有数据。 Mention that I want to do it with textbox without any button.
提到我想用没有任何按钮的文本框来做。 I get this error:
我收到此错误:
Error CS0029 Cannot implicitly convert type 'System.Windows.Forms.BindingSource' to 'System.Windows.Forms.DataGridView'
Here is my code:这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Firma
{
public partial class Cautare : Form
{
public Cautare()
{
InitializeComponent();
}
private void btnCautareRefresh_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\gabya\Documents\Firma.mdf;Integrated Security=True;Connect Timeout=30");
//Citire Stoc
SqlDataAdapter sda = new SqlDataAdapter("Select * from [dbo].[stoc]", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView2.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = item["cod"].ToString();
dataGridView2.Rows[n].Cells[1].Value = item["marca"].ToString();
dataGridView2.Rows[n].Cells[2].Value = item["model"].ToString();
dataGridView2.Rows[n].Cells[3].Value = item["anul"].ToString();
dataGridView2.Rows[n].Cells[4].Value = item["piesa"].ToString();
dataGridView2.Rows[n].Cells[5].Value = item["stoc"].ToString();
}
}
private void txtCautareMarca_TextChanged(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView2.DataSource;
bs.Filter = "marca '%" + txtCautareMarca.Text + "%'";
dataGridView2 = bs;
}
}
}
Just change the last line in the code只需更改代码中的最后一行
from:从:
dataGridView2 = bs;
to:至:
dataGridView2.Datasource = bs;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.