简体   繁体   English

根据C#中另一列的当前行过滤datagridview列

[英]Filter the datagridview column based on the current row of another column in C#

I have a datagridview that is populating columns from different table. 我有一个datagridview,它正在填充来自不同表的列。 I want to filter the column based on another column of the current row. 我想根据当前行的另一列来过滤列。 I tried to use the cell enter event of the datagridview and then filtered the column by filtering the binding source on the column of the current row. 我尝试使用datagridview的单元格enter事件,然后通过过滤当前行的列上的绑定源来过滤该列。

private void lINKDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    this.pROBLEMBindingSource.Filter = "item_id = " + this.lINKDataGridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumn4.Index].Value + "";
}

This is how I am filtering the "problem " binding source on the cell enter event of the datagridview . 这就是我如何过滤datagridview的单元输入事件上的“问题”绑定源。 It is working fine but I am getting an error- being: System.ArgumentException: DataGridViewComboBoxCell value is not valid. 它工作正常,但出现错误:System.ArgumentException:DataGridViewComboBoxCell值无效。

Any suggestion 任何建议

Is item_id field is string type or number type.If it is a string type you have to put in single cote. 是item_id字段是字符串类型还是数字类型。如果是字符串类型,则必须使用单整数。

Well you can use like this 好吧,你可以这样使用

 private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
 {
  // [ Item_id column ] Make sure to use the item_id column index
  if (e.ColumnIndex == 5) 
        {
           userBindingSource.Filter = "Item_Id = " + Convert.ToInt64(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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