【问题标题】:DataGridView Select command with parameters带参数的 DataGridView 选择命令
【发布时间】:2015-06-05 22:00:00
【问题描述】:

基本上我想要的是这样的数据网格视图的填充查询

SELECT DealerName, OrderId, DealerId, OrderDate, ItemType, Price, Quantity, Total, TotalBill FROM dbo.DetailedRecord where DealerName=ComboboxName.SelectedValue

我看不到如何为其添加参数,我不想使用工具条填充

谢谢

【问题讨论】:

  • 你想用这个查询的结果填充你的dataGridView吗?

标签: c# select datagridview


【解决方案1】:

为什么不使用存储过程,然后给它一个数据集来填充你的信息?

Populate DataGridView from a Stored Procedure

【讨论】:

    【解决方案2】:

    假设你想通过一些combobox.selectedvalue过滤数据并且你有一个提交按钮,在那个提交按钮代码中,你初始化了一个yourdatasource.table类型的新数据表

    YourDataSource.YourTableDataTable anything= new YourDataSource.YourTableDataTable();
    
        yourdataadapter.fill(anything,parametervalue.tostring());
         DataGridView1.datasource= anything;
    

    一切准备就绪。

    【讨论】:

      【解决方案3】:

      尝试将表格绑定到您的DataGridView

      下面是一个简单的例子:

      MySqlConnection conn = new MySqlConnection(connectionstring);          
      conn.Open();
      
      string stmt = "SELECT DealerName, OrderId, DealerId, OrderDate, ItemType, Price, Quantity,
      Total, TotalBill FROM dbo.DetailedRecord where DealerName=ComboboxName.SelectedValue";
      
      DataSet ds = new DataSet();
      MySqlDataAdapter da = new MySqlDataAdapter(stmt, conn);
      da.Fill(ds, "dbo.DetailedRecord");           
      dataGridView1.DataSource = ds.Tables["dbo.DetailedRecord"];
      conn.Close();
      

      【讨论】:

        【解决方案4】:

        只需将参数作为字符串传递到查询中。使用简单的字符串连接 (++)。仔细观察您如何创建该搜索字符串。确保首先初始化 DataTable,否则它会弹出关于 null 参数的错误:例如(适用于 SQL Server、Mysql 和 Postgres)

            String connectionString = "server = ...; db= ...; passwd = ....;";
            DataTable dt_reservation_product_mix = new DataTable();
            MySqlDataAdapter ad3;
            ad3  = new MySqlDataAdapter("select `product`.`name`, `product`.`notes` from `product` where `product`.`code` = " + Convert.ToString(ComboboxName.SelectedValue) + "; ", connectionString);
            ad3.Fill(dt_reservation_product_mix);
            ad3.Dispose();
        

        【讨论】:

          猜你喜欢
          • 2020-05-04
          • 1970-01-01
          • 2013-06-04
          • 2012-02-25
          • 1970-01-01
          • 1970-01-01
          • 2014-12-03
          • 2022-10-04
          • 2021-01-26
          相关资源
          最近更新 更多