0

我有一个只显示 2 列的 DataGridView:员工姓名和一个名为参加的复选框。这是为了跟踪员工出席安全会议的情况。

加载此 DGV 时,我希望已标记为已参加的员工在各自的复选框中打勾。

这是我尝试过的,但它只检查所有框:

private void LoadEmpAttendanceDGV()
{
    string sqlstm = "SELECT EmpID, EmpName FROM dbo.MY_TABLE";
    // Additional code here, which loads the grid.

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        myKey.empID = tempEmpID;
        myKey.trainingID = tempTrainingID;
        myRow = trainingSession.Read(myKey);

        // Check to see if record exists, meaning employee attended meeting.
        if (myRow != null)
        {
            for (int j=0; j < ds.Tables[0].Rows.Count; j++)
            {
                myDataGridView["Attended", j].Value = true;
                // Where "Attended" is the name of my CheckBox column
            }
        }
    }
}

如果有人可以提供任何见解,我将不胜感激。

4

4 回答 4

1

像这样创建表

SqlServer 代码

USE [acc]
GO

CREATE TABLE [dbo].[EmpAttendedTheMeeting](
    [EName] [varchar](50) NOT NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Employees](
    [EName] [varchar](50) NOT NULL
) ON [PRIMARY]

GO
insert into Employees(EName) values('kashif')
insert into Employees(EName) values('sunny')
insert into Employees(EName) values('kamran')

insert into EmpAttendedTheMeeting(EName) values('kashif')
insert into EmpAttendedTheMeeting(EName) values('kamran')

c# 代码

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataGridViewTextBoxColumn tb = new DataGridViewTextBoxColumn();
        DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
        private void Form1_Load(object sender, EventArgs e)
        {
            FormatGrid(dataGridView1);
            LoadDg();
        }
        private void FormatGrid(DataGridView dg)
        {
            dg.Columns.Add(tb);
            dg.Columns[0].HeaderText = "EName";
            dg.Columns[0].Width = 199;

            dg.Columns.Add(cb);
            dg.Columns[1].HeaderText = "Attended";
            dg.Columns[1].Width = 69;            
        }
        private void LoadDg()
        {
            SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=acc;uid=sa;pwd=emotions");                
            SqlDataAdapter da = new SqlDataAdapter("select Employees.EName, EmpAttendedTheMeeting.EName from Employees left join EmpAttendedTheMeeting on Employees.EName = EmpAttendedTheMeeting.EName", cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {           
                dataGridView1.Rows.Add(dr[0], dr[1].ToString() == string.Empty ? false : true);
            }
        }   

    }
}

我在我的gridview中得到这样的结果。因为sunny不在EmpAttentendedTheMeeting中,所以他的名字是未选中的

网格视图

于 2013-02-22T23:13:25.700 回答
0

尝试在设置值的底部附近的行中将 j 更改为 i

于 2013-02-22T20:03:10.907 回答
0

如果我错了,请原谅我,但这部分代码:

        for (int j=0; j < ds.Tables[0].Rows.Count; j++)
        {
            myDataGridView["Attended", j].Value = true;
            // Where "Attended" is the name of my CheckBox column
        }

肯定将每一行都设置为真,不是吗?您只想将特定行设置为 true。

于 2013-02-22T23:34:40.980 回答
0

如何在原始 SQL 中捕获记录的存在?就像是

SELECT EmpID, EmpName, isnull(attended,0) FROM dbo.MY_TABLE m 左外连接参加了 m.EmpID = a.EmpID

于 2013-02-22T20:34:40.740 回答