Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Use of Filter field for BindingSource
 

Use of Filter field for BindingSource

I am trying to build a combo / dropdown list in a datagridview control. I have a data source for the drop down, but i need to filter the drop down for each row in the grid. I thought that could use the filter property, but i cant figure the syntax to use.

Can i use the filter property for this, or is the a better way?

regards

Paul
Paul Durdin  Thursday, October 01, 2009 2:05 PM
Hi Paul,

You can use DataTable.DefaultView.RowFilter to do the filter job. But before you do the filter, you need to make a copy of the DataTable, that enable each cell has its own DataSource. If all the cells share the same DataTable as DataSource, you will meet error when filtering. Here is a simple example. Hope it give you some light to do with this case.

public partial class Form1 : Form
    {
        private DataTable dtComboBox = new DataTable();

        public Form1()
        {
            InitializeComponent();
            dtComboBox.Columns.Add("ID", typeof(int));
            dtComboBox.Columns.Add("Name", typeof(string));
            dtComboBox.Rows.Add(0, "Item0");
            dtComboBox.Rows.Add(1, "Item1");
            dtComboBox.Rows.Add(2, "Item2");
            dtComboBox.Rows.Add(3, "Item3");
            dtComboBox.Rows.Add(4, "Item4");

            DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();
            dgvCmb.DataSource = dtComboBox;
            dgvCmb.DisplayMember = "Name";
            dgvCmb.ValueMember = "ID";
            dataGridView1.Columns.Add(dgvCmb);

            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
        }

        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox cmb = e.Control as ComboBox;
            if (cmb != null)
            {
                DataTable dt = dtComboBox.Copy();
                dt.DefaultView.RowFilter = "Name='Item" + dataGridView1.CurrentCell.RowIndex.ToString() + "'";
                cmb.DataSource = dt.DefaultView;

            }
        }
}

In this sample, first I set the DataSource property of DataGridViewComboBoxColumn with the dtComboBox. Then I handle the DataGridView.EditingControlShowing event to get the ComboBox from the cell. Therefore I can do the copy and filter the rows for the copied one and bind it to the ComboBox. In my example, only the item whose index is the same as the current editing cell’s RowIndex will be shown in the drop down list.

If you have any doubt, please feel free to tell me.

Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at EMAIL REMOVED
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Friday, October 02, 2009 7:48 AM

You can use google to search for other answers

Custom Search

More Threads

• Need help populating a DropDownList in and EditItemTemplate
• Data Binding in TierDeveloper
• Problem in using Aggregate function with column and expression
• Form Leaving - check all update done
• Controling forms datasorse through vb
• Problem in double click event of datagrid
• BindingNavigator DeleteItem
• LoadDataRow() Exception: "Input array is longer than the number of columns in this table."
• How do I create an object from a row in a DataGridView / BindingSource?
• Can't if (e.Control.TextChanged != null) , throws exception