Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
AnswerRe: datagridview filtering....where to start? Pin
Pedro Covarrubias7-May-10 19:30
Pedro Covarrubias7-May-10 19:30 
AnswerRe: datagridview filtering....where to start? Pin
Stanciu Vlad7-May-10 22:52
Stanciu Vlad7-May-10 22:52 
GeneralRe: datagridview filtering....where to start? Pin
mprice21420-May-10 10:12
mprice21420-May-10 10:12 
GeneralRe: datagridview filtering....where to start? Pin
Stanciu Vlad20-May-10 10:22
Stanciu Vlad20-May-10 10:22 
GeneralRe: datagridview filtering....where to start? [modified] Pin
mprice21424-May-10 8:24
mprice21424-May-10 8:24 
GeneralRe: datagridview filtering....where to start? Pin
Stanciu Vlad25-May-10 21:12
Stanciu Vlad25-May-10 21:12 
GeneralRe: datagridview filtering....where to start? Pin
mprice21426-May-10 7:48
mprice21426-May-10 7:48 
GeneralRe: datagridview filtering....where to start? Pin
Stanciu Vlad26-May-10 10:05
Stanciu Vlad26-May-10 10:05 
Check this out...
private void Form1_Load(object sender, EventArgs e)
{
    DataTable mainData = new DataTable("mainData");
    mainData.Columns.Add("ID", typeof(int));
    mainData.Columns.Add("ValueType", typeof(int));
    mainData.Columns.Add("ValueUnitOfMeasurement", typeof(int));
    mainData.Columns.Add("TheValue", typeof(decimal));

    DataTable valueType = new DataTable("valueType");
    valueType.Columns.Add("ID", typeof(int)); // should be like a primary key
    valueType.Columns.Add("Description", typeof(string));

    DataTable valueUnitOfMeasurement = new DataTable("valueUnitOfMeasurement");
    valueUnitOfMeasurement.Columns.Add("ID", typeof(int)); // should be like a primary key
    valueUnitOfMeasurement.Columns.Add("ValueTypeID", typeof(int));
    valueUnitOfMeasurement.Columns.Add("Description", typeof(string));

    valueType.Rows.Add(new object[] { 1, "Force" });
    valueType.Rows.Add(new object[] { 2, "Torque" });
    valueType.Rows.Add(new object[] { 3, "Pressure" });

    valueUnitOfMeasurement.Rows.Add(new object[] { 1, 1, "lb"});
    valueUnitOfMeasurement.Rows.Add(new object[] { 2, 1, "N" });
    valueUnitOfMeasurement.Rows.Add(new object[] { 3, 2, "N-m" });
    valueUnitOfMeasurement.Rows.Add(new object[] { 4, 2, "in-lb" });
    valueUnitOfMeasurement.Rows.Add(new object[] { 5, 3, "Pa" });
    valueUnitOfMeasurement.Rows.Add(new object[] { 6, 3, "bar" });


    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.DataSource = mainData;

    valueTypeColumn.DataSource = valueType;
    valueTypeColumn.ValueMember = "ID";
    valueTypeColumn.DisplayMember = "Description";

    myBinding.DataSource = valueUnitOfMeasurement;
    valueUnitOfMeasurementColumn.DataSource = myBinding;
    valueUnitOfMeasurementColumn.ValueMember = "ID";
    valueUnitOfMeasurementColumn.DisplayMember = "Description";

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

void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
    myBinding.Filter = string.Empty; // reset the filter
}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is ComboBox) // if the editing control is a comboBox
    {
        ComboBox editingControl = e.Control as ComboBox;

        // check if the comboBox is the one binded to the binding source
        if (editingControl.DataSource is BindingSource)
        {
            if ((editingControl.DataSource as BindingSource).Equals(myBinding))
            {
                // this control is binded to the bindingSource, so let's filter the binding source

                if(dataGridView1.CurrentRow == null)
                    return;

                int selectedID;
                bool ok = Int32.TryParse(Convert.ToString(dataGridView1.CurrentRow.Cells["valueTypeColumn"].Value), out selectedID);

                if (ok) // do the filter
                    myBinding.Filter = "ValueTypeID = " + selectedID;
                else // no value selected
                    myBinding.Filter = "ValueTypeID = -1"; // show nothing


                return;
            }
        }
    }
}

I have no smart signature yet...

GeneralRe: datagridview filtering....where to start? Pin
mprice21426-May-10 17:43
mprice21426-May-10 17:43 
GeneralRe: datagridview filtering....where to start? Pin
Stanciu Vlad26-May-10 20:43
Stanciu Vlad26-May-10 20:43 
GeneralRe: datagridview filtering....where to start? <Solved> Pin
mprice21426-May-10 18:52
mprice21426-May-10 18:52 
GeneralRe: datagridview filtering....where to start? Pin
Stanciu Vlad26-May-10 20:54
Stanciu Vlad26-May-10 20:54 
GeneralRe: datagridview filtering....where to start? Pin
mprice21427-May-10 3:13
mprice21427-May-10 3:13 
GeneralRe: datagridview filtering....where to start? Pin
Stanciu Vlad27-May-10 6:52
Stanciu Vlad27-May-10 6:52 
QuestionXML Serialization of derived classes Pin
Roland Bär7-May-10 12:35
Roland Bär7-May-10 12:35 
AnswerRe: XML Serialization of derived classes Pin
Peace ON7-May-10 21:40
Peace ON7-May-10 21:40 
AnswerRe: XML Serialization of derived classes Pin
Stanciu Vlad7-May-10 23:20
Stanciu Vlad7-May-10 23:20 
Questionhow can i make a distributed system through C# ? Pin
Med7at7-May-10 11:30
Med7at7-May-10 11:30 
AnswerRe: how can i make a distributed system through C# ? Pin
Garth J Lancaster7-May-10 13:43
professionalGarth J Lancaster7-May-10 13:43 
AnswerRe: how can i make a distributed system through C# ? Pin
Member 41702067-May-10 15:04
Member 41702067-May-10 15:04 
GeneralRe: how can i make a distributed system through C# ? Pin
Med7at7-May-10 15:47
Med7at7-May-10 15:47 
AnswerRe: how can i make a distributed system through C# ? Pin
Peace ON7-May-10 19:44
Peace ON7-May-10 19:44 
Questionreturn number of records selected Pin
Abdul-Rhman Alsri7-May-10 11:21
Abdul-Rhman Alsri7-May-10 11:21 
AnswerRe: return number of records selected Pin
Dr.Walt Fair, PE7-May-10 12:27
professionalDr.Walt Fair, PE7-May-10 12:27 
AnswerRe: return number of records selected Pin
Member 41702067-May-10 15:07
Member 41702067-May-10 15:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.