简体   繁体   English

如果记录存在,如何用Check in DataGridView填充CheckBox列?

[英]How to populate CheckBox column with Check in DataGridView if record exists?

I have a DataGridView that only displays 2 columns: Employee Name, and a checkbox called Attended. 我有一个仅显示2列的DataGridView:“雇员姓名”和一个名为“出席”的复选框。 This is to keep track of employee attendance at safety meetings. 这是为了跟踪员工参加安全会议的情况。

When this DGV is loaded, I would like the employees who have already been marked as having attended to have a check mark in their respective checkboxes. 加载此DGV时,我希望已被标记为已参加会议的员工在其各自的复选框中都选中一个勾号。

This is what I tried, but it just checks ALL boxes: 这是我尝试过的方法,但只选中了所有复选框:

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
            }
        }
    }
}

If anyone can offer any insight, I would greatly appreciate it. 如果有人可以提供任何见解,我将不胜感激。

Create tables like this 像这样创建表

SqlServer Code 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# Code 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);
            }
        }   

    }
}

Im getting the result in my gridview like this. 我在这样的网格视图中得到结果。 because sunny is not in EmpAttentendedTheMeeting, his name is unchecked 因为Sunny不在EmpAttentendedTheMeeting中,所以未选中他的名字

网格视图

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

How about catching the existance of the record in the original SQL ? 如何在原始SQL中捕获记录的存在? Something like 就像是

SELECT EmpID, EmpName, isnull(attended,0) FROM dbo.MY_TABLE m left outer join attended a on m.EmpID = a.EmpID SELECT EmpID,EmpName,isnull(attended,0)来自dbo.MY_TABLE m左外部联接在m.EmpID = a.EmpID上参加

Forgive me if I'm wrong but this section of the code: 如果我错了,请原谅我,但是这段代码:

        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
        }

Surely sets every row to true, doesn't it? 当然将每一行都设置为true,不是吗? You just want to set the specific row to true. 您只想将特定行设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查列是否存在于DataGridView中 - Check if column exists in DataGridView 如何检查datagridview列中的复选框是否已选中 - how to check whether checkbox is checked in datagridview column 如果datagridview列的值=“ True”,如何选中复选框 - How to check checkbox if the datagridview column value=“True” 如何将组合框或复选框添加到“ Datagridview”的现有记录中而不是在新列中 - How to add combobox or checkbox to existing record of “Datagridview” not in a new column 如何在 datagridview 中制作一列复选框,填充更新表单上的复选框? - How do you make a column in the datagridview that is a checkbox populate a checkbox on an update form? 如何禁用DataGridView CheckBox列中的特定复选框单元格 - How to disable particular check box cell in a DataGridView CheckBox column 如何检查是否选中了 dataGridView checkBox? - How to check if dataGridView checkBox is checked? 如何根据表中的现有数据记录选中或取消选中 Datagridview 中的复选框 - How to Check or Uncheck a Checkbox in Datagridview based on existing data record from Table 如何有效地在 DataGridView 中填充生成的列? - How to populate a generated Column in a DataGridView efficiently? 如何在DataGridView中填充自定义列单元格 - How to populate custom column cells in DataGridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM