【问题标题】:Additional information: Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'附加信息:无法将“System.Windows.Forms.BindingSource”类型的对象转换为“System.Data.DataTable”类型
【发布时间】:2013-12-11 13:52:43
【问题描述】:

我的代码有一个奇怪的问题。我目前正在为我的数据网格编写一个过滤器。

每当用户清除文本字段时 - 都会出现以下错误消息:

无法将“System.Windows.Forms.BindingSource”类型的对象转换为 输入“System.Data.DataTable”。

这是我目前的代码:

    private void driverNo_TextChanged(object sender, EventArgs e)
    {

        // if driverNo text is empty then return all rows

        if (string.IsNullOrEmpty(driverNo.Text))
        {
            ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = string.Empty;
            return;
        }

        // if driverNo is a numerical value then view result

        int temp;
        if (int.TryParse(driverNo.Text, out temp))
            ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "DriverNo = " + driverNo.Text;
        else
            MessageBox.Show("Invalid driver number.");
            driverNo.Text = "";
    }

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    DataSource 的值是BindingSource 的类型,而不是DataTable。基本上你的期望是不正确的。 DataTable 可能实际上支持 BindingSource

    您的 WinForms 设计图面上很可能有一个BindingSource 组件。您可以通过以下方式进入餐桌:

    var bindingSource = this.myBindingSource;
    var dt = (DataTable)bindingSource.DataSource;
    

    您可以通过以下方式间接获得它:

    var bindingSource = (BindingSource)dataGridView1.DataSource;
    var dt = (DataTable)bindingSource.DataSource;
    

    对于您的代码,它可能如下所示:

    private void driverNo_TextChanged(object sender, EventArgs e)
    {
    
        // if driverNo text is empty then return all rows
    
        if (string.IsNullOrEmpty(driverNo.Text))
        {
            var bindingSource = (BindingSource)dataGridView1.DataSource.
            var table = (DataTable)bindingSource.DataSource;
            table.DefaultView.RowFilter = string.Empty;
            return;
        }
    
        // if driverNo is a numerical value then view result
    
        int temp;
        if (int.TryParse(driverNo.Text, out temp))
        {
            var bindingSource = (BindingSource)dataGridView1.DataSource.
            var table = (DataTable)bindingSource.DataSource;
            table.DefaultView.RowFilter = "DriverNo = " + driverNo.Text;
        }
        else
            MessageBox.Show("Invalid driver number.");
            driverNo.Text = "";
    }
    

    【讨论】:

    • @methuselah 它看起来像上面,而不是((DataTable)dataGridView1.DataSource)
    • 通过上述 - 我收到错误消息:Error 1 'System.Windows.Forms.BindingSource' does not contain a definition for 'DefaultView' and no extension method 'DefaultView' accepting a first argument of type 'System.Windows.Forms.BindingSource' could be found (are you missing a using directive or an assembly reference?)
    • @methuselah 给我一分钟,我会更新我的答案。
    【解决方案2】:
    DataTable dt = ((DataView)gridView1.DataSource).Table;
    
    DataView dv = (DataView)gridView1.DataSource;
    

    【讨论】:

      【解决方案3】:
              `xxxBindingSource.AddNew();`
              `dataGridView1.CurrentRow.Cells[0].Value = "Value Here";`
              `dataGridView1.CurrentRow.Cells[1].Value = "Value here";`
              `xxxBindingSource.EndEdit();`
      

      【讨论】:

      • 请解释您的代码。并且您的代码块的格式已关闭。
      • 建议的代码需要解释它与问题的关系。
      猜你喜欢
      • 2018-02-14
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多