简体   繁体   中英

How to display sql data in data grid view when a text and id is entered in a text box

下面是数据库中的sql数据。

这是我的胜利形式

How to display sql data in the data grid view when a text is entered in text box.for eg:there is wai wai and britannia in dBase.I want to display all the information of data base in data grid column when product_name or product_id is entered in the Product Name: textbox.I've tried the following code: but its not working:

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyData==Keys.Enter)
    {
        SqlConnection con = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True");
        SqlDataAdapter a = new SqlDataAdapter("select product_Name,product_Id,purchase_Price,discount,beforevat_Price,vat_Rate,actual_Cost,Margin,actual_Sp from Product", con);
        DataTable b = new DataTable();
        a.Fill(b);
        dataGridView1.DataSource = b;
        textBox3.Focus();
    }
}

it displays output like below: 在此处输入图片说明

You can actually do it this way

string WhrCond = "";
int n;
if(int.TryParse(textBox2.Text, out n))
{
    WhrCond = "product_Id = " + textBox2.Text.Trim() ;
}
else
{
    WhrCond = "product_Name LIKE '%" + textBox2.Text.Trim() + "%'";
}

if(string.IsNullOrEmpty(textBox2.Text))
{
    WhrCond = "1 = 1";
}
SqlConnection con = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True");
SqlDataAdapter a = new SqlDataAdapter("select product_Name as [Product Name],
                        actual_Cost as [Actual Cp], 
                        Margin, 
                        actual_Sp as [Actual SP], 
                        product_Id as [Product Id], 
                        purchase_Price as [Purchase Price], 
                        discount as [Discount], 
                        beforevat_Price as [Before Vat Price],
                        vat_Rate as [Vat%] from Product WHERE " + WhrCond , con);
DataTable b = new DataTable();
a.Fill(b);
dataGridView1.DataSource = b;
textBox3.Focus();

You shall be removing all the data from DGV created on the design time.

I'm assuming your datagridview have no column set at start here (since we use datasource) try erase them.

 private void textBox2_TextChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True " + "connection timeout=300");
            conn.Open();

            string query = "Select * from Product where product_Name  like '%"+textBox2.Text+"%'";
            SqlDataAdapter a = new SqlDataAdapter(query,conn);
            DataTable b = new DataTable();
            a.Fill(b);
            dataGridView1.DataSource = b;

        }

PS: if you absolutely want set column at start then don't use DataSource but use foreach loop on datatable b.rows then add the rows to datagridview1

DtataGridView1.Rows.Add(b["Product_Name],..,...)

2-like lei said you can use DataPropertyNametoo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM