I want Twice binding datagrid but get following error:

Cannot bind to the property or column users on the DataSource

Recommended Answers

All 4 Replies

Wow since none of us are psychics and/or mind readers would you please post some code in code tags and maybe we could help.

Hi

My code is :

private void Account_Load(object sender, EventArgs e)
        {
            //this code bind datagrid and make farsi header datagrid

            DataSet ds = new DataSet();
            ds=Classes.SelectQuery("select id,F_name,L_name,username from users", "users");
            dataGridView1.DataBindings.Add(new Binding("datasource",ds,"users"));
            dataGridView1.DataBindings.Clear();
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].HeaderText = "نام";
            dataGridView1.Columns[2].HeaderText = "نام خانوادگی";
            dataGridView1.Columns[3].HeaderText = "نام کاربری";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count != 0)
            {
                if (MessageBox.Show("آیا مطمئن هستید که می خواهید این سطر را پاک کنید ؟", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    int id;
                    id = (int)dataGridView1.CurrentRow.Cells[0].Value;
                    DataSet ds = new DataSet();
                    ds=Classes.SelectQuery("DELETE FROM users WHERE id='" + id + "'", "usres");
                    dataGridView1.DataBindings.Add(new Binding("datasource",ds,"users"));
                }
            }
            else
            {
                MessageBox.Show("سطری برای پاک کردن موجود نمی باشد ", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

This is my class:

public static DataSet Query(string str,string table)
        {
            MySqlCommand cmd = new MySqlCommand();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            cmd.CommandText = str;
            cmd.Connection = localcon;
            localcon.Open();
            cmd.ExecuteNonQuery();
            localcon.Close();
            return ds;

        }
        public static DataSet SelectQuery(string str, string table)
        {
            MySqlCommand cmd = new MySqlCommand();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            cmd.CommandText = str;
            cmd.Connection = localcon;
            localcon.Open();
            cmd.ExecuteNonQuery();
            ds.Clear();
            da.Fill(ds, table);
            localcon.Close();
            return ds;

        }

Well for starters

DataSet ds = new DataSet();
            ds=Classes.SelectQuery("select id,F_name,L_name,username from users", "users");

Should be:

DataSet ds = ds=Classes.SelectQuery("select id,F_name,L_name,username from users", "users");

Your method returns a DataSet so the one you are creating in the _Load event is merely discarded right after you create it.

Next:

dataGridView1.DataBindings.Add(new Binding("datasource",ds,"users"));
            dataGridView1.DataBindings.Clear();

Should be:

dataGridView1.DataBindings.Clear();
            dataGridView1.DataBindings.Add(new Binding("datasource",ds,"users"));

I don't use the .DataBindings. property but I can't imagine anything useful happening out of you adding a DataBinding then clearing it right away.

If that doesn't fix it i'm not sure what will. You didn't indicate where the problem is happening in your code so i'm reluctant to try it.

I think you also have a typo:

ds=Classes.SelectQuery("DELETE FROM users WHERE id='" + id + "'", "[B]usres[/B]");

Thank you.
I got answer.

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.