在另一个表单的dataGridView中显示查询结果

时间:2013-08-06 12:58:59

标签: c# winforms ms-access datagridview

我正在使用在Access中创建的本地数据库,我将其作为DataSource添加到我的C#项目中。我有两种表单:RoomSelectRoomActiveSession

RoomSelect包含一个包含4个值(房间号)和一个按钮的列表框。用户选择房间号,单击“确定”,然后应重定向到RoomActiveSession表单,其中给定房间号的活动会话应显示在DGV中。

RoomActiveSession包含dataGridView以显示结果。

我的问题是:如何从RoomSelect正确访问DGV以显示查询结果?

RoomSelect代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoReg
{
    public partial class RoomSelect : Form
    {

        DataTable queryResult = new DataTable();
        public string RoomID;
        RoomActiveSession RoomActiveSessionForm = new RoomActiveSession();

        public RoomSelect()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            switch (listBox1.SelectedItem.ToString())
            {
                case "MB0302":
                    RoomID = listBox1.SelectedItem.ToString();
                    RoomActiveSessionForm.ShowDialog();
                    roomQuery();
                    break;
                case "MC1001":
                    RoomID = listBox1.SelectedItem.ToString();
                    RoomActiveSessionForm.ShowDialog();
                    roomQuery();
                    break;
                case "MC3203":
                    RoomID = listBox1.SelectedItem.ToString();
                    RoomActiveSessionForm.ShowDialog();
                    roomQuery();
                    break;
                case "MC3204":
                    RoomID = listBox1.SelectedItem.ToString();
                    RoomActiveSessionForm.ShowDialog();
                    roomQuery();
                    break;

            }
        }

        public void roomQuery()
        {
            string ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Kacper\\Desktop\\AutoReg\\AutoReg\\AutoReg.accdb;";

            OleDbConnection MyConn = new OleDbConnection(ConnStr);
            MyConn.Open();

            //SQL query that todays sessions for the given roomID
            string query = @"SELECT SessionID, SemesterA, SemesterB, RoomID, SessionDate, SessionTimeStart, SessionTimeEnd" +
               " FROM [Session] " +
               " WHERE RoomID = @RoomID " +
               " AND SessionDate = Date() ";

            OleDbCommand command = new OleDbCommand(query, MyConn);

            command.Parameters.Add("RoomID", OleDbType.Char).Value = RoomID;


            OleDbDataAdapter adapter = new OleDbDataAdapter(command);

            adapter.Fill(queryResult);

            if (queryResult.Rows.Count == 0)
            {
                MessageBox.Show("No active sessions today for the given room number");
                MyConn.Close();
            }
            else
            {

                RoomActiveSession.dataGridView1.DataSource = queryResult;
                MyConn.Close();
            }


        }

    }
}

我收到错误:RoomActiveSession.dataGridView1.DataSource = queryResult;

'AutoReg.RoomActiveSession.dataGridView1' is inaccessible due to its protection level   C:\Users\Kacper\Desktop\AutoReg\AutoReg\RoomSelect.cs

根据这篇文章datagird access from another form我应该在RoomActiveSession创建get,设置DGV的属性,但我不知道该怎么做(我应该修改RoomActiveSession中的代码设计师?)

1 个答案:

答案 0 :(得分:2)

dataGridView1修饰符更改为public。您可以选择dataGridView1并在Modifiers窗口中设置Properties

要创建属性以访问dataGridView1,请执行以下操作:(我认为您只需要访问权限,不允许修改它):

public class RoomActiveSession : Form {
   //.....
   public DataGridView Grid {
      get { return dataGridView1; }
   }
}
//you can keep your dataGridView1 modifier as private

您还可以定义一些公共方法,以便对dataGridView1执行某些操作,例如SetDataSource

public void SetDataSouce(object source){
    dataGridView1.DataSource = source;
}

有许多方法可以与班级dataGridView1外的RoomActiveSession进行互动。