Hi there

My first forum post... internet being a luxury for me and all. Okay my program is a simple database management system connecting to an access database. however i am having one annoying problem, pertaining to a datagrid and a list box. they cannot be data bound as this gives problems,besides the code works but only if a message box is used for the list box code just before running its little bit, check for comment in list box code. Can you please explain to me why this works only for a message box appearal and if there is another way to get it to work as the message box is rather unwanted. If it is not used, simply nothing happens.

bool switcher = true;

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            dataGridView1.Refresh();
            if (dataGridView1.CurrentRow != null && switcher)
            {
                int selectedrow = dataGridView1.CurrentRow.Index;

                string currentpack = dataGridView1[2, selectedrow].Value.ToString();

                if (currentpack == "1Kg")
                {
                    radioButton1.Checked = true;
                }
                if (currentpack == "2Kg")
                {
                    radioButton2.Checked = true;
                }
                comboBox1.SelectedValue = dataGridView1[1, selectedrow].Value;
            }
        }

private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            int numrows = 0;
            switcher = false;
            MessageBox.Show(""); //This is what makes this work... Why?
            if (comboBox1.SelectedValue != null)
            {
                string biscuitTpe = comboBox1.SelectedText;
                string size;
                if (radioButton1.Checked)
                    size = "1Kg";
                else
                    size = "2Kg";
                while (numrows < dataGridView1.RowCount)
                {
                    if (dataGridView1[1, numrows].Value.ToString().Equals(biscuitTpe) && dataGridView1[2, numrows].Value.ToString().Equals(size))
                    {
                        dataGridView1.Rows[numrows].Selected = true;
                    }
                numrows++;
                }
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            switcher = true;
        }

Much Much appreciated

Recommended Answers

All 6 Replies

OK, why cant the list box and grid be bound, you mention it gives problems - what were the problems?

As for why your message box makes it work, thats hard to say, other than either something is happening time related, or, perhaps instead of selected value != null, you might want to test if the index is >-1

Alright, I have 2 tables... Biscuits, and Packages. Now i want to edit the cost of the package, so i'm working in the packages table, however for each biscuitType there are two packages one for each size (1kg)&(2kg) of package, biscuitType being my primary key in Biscuits table and packageId in Packages table, they are linked by the foreign key of biscuit type in Packages. So if a databind the listbox to the grid view i will have twice the biscuitType in my list box, for each package size. I will try the -1 index though.

Thanx for your help.

No, because the listbox should be linked directly to the biscuits table, so would only have 1 entry

Ja it is bound to the biscuit table. But by doing that it does not automatically change the datagrid (bound to packages) selection when changing the list box value :(

Ok, but the reason it doesnt change is as you move through one table you have to tell the listbox that you've changed value..

If you look you your dataset designer, you'll find you can relate tables, see if that helps.

LizR you have taught me something new :) Thank you. Still not quite what i had in mind, but it will get the job done. Thank you very much! Maybe i can play with it and be surprised. Thanx again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.