Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文本框中输入文本和id时,如何在数据网格视图中显示sql数据_C#_Sql_Sql Server - Fatal编程技术网

C# 在文本框中输入文本和id时,如何在数据网格视图中显示sql数据

C# 在文本框中输入文本和id时,如何在数据网格视图中显示sql数据,c#,sql,sql-server,C#,Sql,Sql Server,当在文本框中输入文本时,如何在数据网格视图中显示sql数据。例如:dBase中有wai wai和britannia。当在产品名称:文本框中输入产品名称或产品id时,我想在数据网格列中显示数据库的所有信息。我尝试了以下代码:但它不起作用: private void textBox2_KeyDown(object sender, KeyEventArgs e) { if(e.KeyData==Keys.Enter) { SqlConnection con = new

当在文本框中输入文本时,如何在数据网格视图中显示sql数据。例如:dBase中有wai wai和britannia。当在产品名称:文本框中输入产品名称或产品id时,我想在数据网格列中显示数据库的所有信息。我尝试了以下代码:但它不起作用:

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();
    }
}
它显示如下输出:

你实际上可以这样做

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();

您将从设计时创建的DGV中删除所有数据。

您实际上可以这样做

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();

您将从设计时创建的DGV中删除所有数据。

我假设您的datagridview在开始时没有设置列(因为我们使用datasource),请尝试删除它们

 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:如果您绝对希望在开始时设置列,那么不要使用DataSource,而是 对datatable b.rows使用foreach循环,然后将这些行添加到datagridview1

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

2-就像lei说的那样,您也可以使用DataPropertyNametoo

我假设您的datagridview在开始时没有设置列(因为我们使用datasource),请尝试删除它们

 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:如果您绝对希望在开始时设置列,那么不要使用DataSource,而是 对datatable b.rows使用foreach循环,然后将这些行添加到datagridview1

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

2-正如lei所说,您也可以使用DataPropertyName

您应该设置列的DataPropertyName以绑定到表。您应该设置列的DataPropertyName以绑定到表。Mohit if(isNumeric(textBox2.Text))不工作。调试时,它显示isNumeric不存在于当前centextnot working中。它显示的输出与我在上述问题中发布的相同。我的datagrid视图有列,当我在文本框2中按enter键时,它会在数据网格视图中添加相同的列。您将从设计时创建的DGV中删除所有数据。否。我希望数据显示在我在DGV中创建的列中。例如,如果我在textbox2中按w,则从w开始的所有项目都应显示在DGV的已设计列中,而不是其他列。非常欢迎您。您也可以通过向上投票并接受答案来表示感谢。Mohit if(isNumeric(textBox2.Text))不工作。当我调试时,它显示isNumeric不存在于当前centextnot working中。它显示的输出与我在上述问题中发布的相同。我的datagrid视图有列,当我在文本框2中按enter键时,它会在数据网格视图中添加相同的列。您将从设计时创建的DGV中删除所有数据。否。我希望数据显示在我在DGV中创建的列中。例如,如果我在textbox2中按w,则从w开始的所有项目都应显示在DGV的已设计列中,而不是其他列。非常欢迎您。你也可以通过投票和接受答案来表示感谢。