[英]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.