将数据绑定到gridview后,它将不会在网格视图中的表上显示

时间:2013-12-13 13:47:46

标签: c# sql .net winforms

我在c#中创建Windows应用程序。在该应用程序中,我想连接两个表并在网格视图中将其显示为单个表。将数据源绑定到gridview后,网格视图将为
没有在表格上显示....这是我的代码

private void btCheckSupplier_Click_1(object sender, EventArgs e)
    {
        string checkSupplier = tbProdcutId.Text;
        try
        {
            DataSet checkSupplierDataSet = new DataSet();
            DataSet n = dal.SupplierDetail(checkSupplierDataSet);

            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = n.DefaultViewManager;
            if (dataGridView2.Rows.Count == 0)
            {

                dataGridView2.AutoGenerateColumns = true;
                dataGridView2.DataSource = bindingSource;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    static string conn = @"Data Source=MyComputer-PC\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";

    public string CheckSupplier(string checkSupplier)//string checkSupplier
    {
        try
        {
            string checkString = "select [Products].ProductID,[Products].ProductName,[Suppliers].SupplierID,[Suppliers].CompanyName,[Suppliers].ContactName,[Suppliers].ContactTitle,[Suppliers].Address,[Suppliers].City,[Suppliers].Region,[Suppliers].PostalCode,[Suppliers].Country,[Suppliers].Phone,[Suppliers].Fax,[Suppliers].HomePage from [Products] Inner Join [Suppliers] On [Products].SupplierID = [Suppliers].SupplierID Where [Products].ProductID ='" + checkSupplier + "'";
            SqlCommand sqlcom = new SqlCommand(checkString, connect);
            connect.Open();
            sqlcom.Connection = connect;
            sqlcom.ExecuteNonQuery();
            connect.Close();
            return checkString;
        }
        catch (Exception)
        {

            throw;
        }

    }

    public DataSet SupplierDetail(DataSet supplierDetail)
    {
        try
        {
            SqlConnection connect = new SqlConnection(conn);
            Class2 checkObj = new Class2();
            connect.Open();               
            SqlCommand sqlCom = connect.CreateCommand();
            sqlCom.CommandText =checkObj.CheckSupplier(checkString);here checkString is called From CheckSupplier method.
            SqlDataAdapter sqlAd = new SqlDataAdapter();
            sqlAd.SelectCommand=sqlCom;                
            sqlAd.Fill(supplierDetail);
            connect.Close();
            return supplierDetail;
        }
        catch (Exception)
        {

            throw;
        }
    }

0 个答案:

没有答案