0

我正在使用在 Access 中创建的本地数据库,我将其作为数据源添加到我的 C# 项目中。我有 2 种形式:RoomSelectRoomActiveSession.

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

RoomActiveSession 包含用于显示结果的 dataGridView。

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

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

根据this post datagird access from another form我应该为DGV创建get,set属性RoomActiveSession,但我不知道该怎么做(我应该在RoomActiveSession设计器中修改代码吗?)

4

1 回答 1

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

于 2013-08-06T13:24:18.143 回答