根据所选按钮显示datagridview记录

时间:2016-05-03 07:27:06

标签: c# datagridview

我在c#中创建一个表单,其中datagridveiw根据选择的楼层显示数据库中的记录。与此enter image description here

相同

问题出在我创建代码之后,我总是收到错误:ColumnCount property cannot be set on a data-bound DataGridView control。 任何人都可以告诉我如何根据所选按钮显示数据库记录。

无论如何,这是我的代码如下。

private void button4_Click(object sender, EventArgs e)
        {
            using (NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=1234;Database=postgres;"))
            {
                using (NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM information WHERE college = 'cbaa' AND floor = '1'", conn))
                {
                    cmd.CommandType = CommandType.Text;
                    using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            da.Fill(dt);

                            dataGridView2.AutoGenerateColumns = false;
                            dataGridView2.ColumnCount = 5;

                            dataGridView2.Columns[0].Name = "roomNum";
                            dataGridView2.Columns[0].HeaderText = "Room No.";
                            dataGridView2.Columns[0].DataPropertyName = "roomNum";

                            dataGridView2.Columns[1].Name = "type";
                            dataGridView2.Columns[1].HeaderText = "Room Type";
                            dataGridView2.Columns[1].DataPropertyName = "type";

                            dataGridView2.Columns[2].Name = "sIllumination";
                            dataGridView2.Columns[2].HeaderText = "Standard Illumination";
                            dataGridView2.Columns[2].DataPropertyName = "sIllumination";

                            dataGridView2.Columns[3].Name = "aIllumination";
                            dataGridView2.Columns[3].HeaderText = "Actual Illumination";
                            dataGridView2.Columns[3].DataPropertyName = "aIllumination";

                            dataGridView2.Columns[4].Name = "difference";
                            dataGridView2.Columns[4].HeaderText = "Difference";
                            dataGridView2.Columns[4].DataPropertyName = "difference";
                            dataGridView2.DataSource = dt;
                        }
                    }

                }
            }    
        }
private void button5_Click(object sender, EventArgs e)
        {
            using (NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=1234;Database=postgres;"))
            {
                using (NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM information WHERE college = 'cbaa' AND floor = '2'", conn))
                {
                    cmd.CommandType = CommandType.Text;
                    using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            da.Fill(dt);

                            dataGridView2.AutoGenerateColumns = false;
                            dataGridView2.ColumnCount = 5;

                            dataGridView2.Columns[0].Name = "roomNum";
                            dataGridView2.Columns[0].HeaderText = "Room No.";
                            dataGridView2.Columns[0].DataPropertyName = "roomNum";

                            dataGridView2.Columns[1].Name = "type";
                            dataGridView2.Columns[1].HeaderText = "Room Type";
                            dataGridView2.Columns[1].DataPropertyName = "type";

                            dataGridView2.Columns[2].Name = "sIllumination";
                            dataGridView2.Columns[2].HeaderText = "Standard Illumination";
                            dataGridView2.Columns[2].DataPropertyName = "sIllumination";

                            dataGridView2.Columns[3].Name = "aIllumination";
                            dataGridView2.Columns[3].HeaderText = "Actual Illumination";
                            dataGridView2.Columns[3].DataPropertyName = "aIllumination";

                            dataGridView2.Columns[4].Name = "difference";
                            dataGridView2.Columns[4].HeaderText = "Difference";
                            dataGridView2.Columns[4].DataPropertyName = "difference";
                            dataGridView2.DataSource = dt;
                        }
                    }

                }
            }     
        }

提前致谢!

0 个答案:

没有答案