将数据网格视图中使用的复选框的值从“true”更改为我自己选择的文本值
本文关键字:true 文本 选择 我自己 视图 网格 复选框 数据 数据网 | 更新日期: 2023-09-27 17:56:09
private void attendence_Load(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection(cls_Connection.connection);
conn.Open();
SqlCommand cmd = new SqlCommand("select type from emp_type",conn);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
atten_cmb_type.Items.Add(reader["type"]);
}
reader.Close();
conn.Close();
}
private void atten_cmb_id_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sq = new SqlConnection(cls_Connection.connection);
sq.Open();
SqlDataAdapter ss = new SqlDataAdapter();
ss.SelectCommand = new SqlCommand("select id,Name,type from Employee_Details where type='"+atten_cmb_type.Text+"'", sq);
DataTable dt = new DataTable();
ss.Fill(dt);
dgv_attendence.Visible = true;
dgv_attendence.DataSource = dt;
sq.Close();
}
string id = "";
string name = "";
string type = "";
string date = "";
string month = "";
string attendance = "";
string year = "";
int a = 0;
private void button1_Click(object sender, EventArgs e)
{
id = lbl_Id.Text;
name = lbl_Name.Text;
type = atten_cmb_type.Text;
date = dateTimePicker1.Value.ToString("dd");
month = dateTimePicker1.Value.ToString("MMMM");
year = dateTimePicker1.Value.ToString("yyyy");
int j = 0;
foreach (DataGridViewRow row in dgv_attendence.Rows)
{
if (row.Cells[j].Value != null)
{
if ((Boolean)row.Cells[j].Value == true)
{
attendance = "Present";
}
else if ((Boolean)row.Cells[j].Value == false)
{
attendance = "Absent";
}
}
j++;
}
SqlConnection con = new SqlConnection(cls_Connection.connection);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("Select * from attendence where id = '" + id+"'" + " AND date = '" + date+"'",con);
con.Open();
DataTable dt = new DataTable();
sda.SelectCommand.ExecuteNonQuery();
sda.Fill(dt);
if (dt.Rows.Count < 1)
{
for (int i = 0; i < dgv_attendence.Rows.Count;i++)
{
SqlCommand cmd = new SqlCommand("insert into attendence(id,Name,type,date,month,attendence,year) values('" + dgv_attendence.Rows[i].Cells[1].Value + "','" + dgv_attendence.Rows[i].Cells[2].Value + "','" + dgv_attendence.Rows[i].Cells[3].Value + "','" + date + "','" + month + "','" + attendance + "','" + year + "')", con);
cmd.ExecuteNonQuery();
}
MessageBox.Show("success");
con.Close();
}
else
{
MessageBox.Show("Record already exists");
}
}
这是我的代码。 首先,我从 3 列 1 employee_details表中填充 GridView。考勤表中也存在同一列。现在,当我单击按钮时,这三列将按原样转到考勤表,而其余列则取自各种控件
你使用Cells[0]是对的。 问题是你没有对你的结果做任何事情。 因此,您遍历所有这些,然后出勤率将等于最后一行的值。
更改循环以保留所有出勤值的列表。 如果表中有一个您更新的单元格会更好,但这将起作用:
List<string> listAttendance = new List<string>();
foreach (DataGridViewRow row in dgv_attendence.Rows)
{
if (row.Cells[0].Value != null && (Boolean)row.Cells[0].Value == true )
{
attendance = "Present";
}
else
{
attendance = "Absent";
}
listAttendance.Add( attendance);
}
然后,您的cmd将更改为使用"listAttendance[i]"而不是"attendance"。
SqlCommand cmd = new SqlCommand("insert into attendence(id,Name,type,date,month,attendence,year) values('" + dgv_attendence.Rows[i].Cells[1].Value + "','" + dgv_attendence.Rows[i].Cells[2].Value + "','" + dgv_attendence.Rows[i].Cells[3].Value + "','" + date + "','" + month + "','" + listAttendance[i] + "','" + year + "')", con);
或者,您可以将字符串逻辑移动到写入数据库的循环中:(并完全摆脱另一个 for 循环,您可以在其中使用"存在"和"缺席")
for (int i = 0; i < dgv_attendence.Rows.Count;i++)
{
if ( dgv_attendence.Rows[i].Cells[0].Value != null && (Boolean)dgv_attendence.Rows[i].Cells[0].Value == true)
{
attendance = "Present";
}
else
{
attendance = "Absent";
}
SqlCommand cmd = new SqlCommand("insert into attendence(id,Name,type,date,month,attendence,year) values('" + dgv_attendence.Rows[i].Cells[1].Value + "','" + dgv_attendence.Rows[i].Cells[2].Value + "','" + dgv_attendence.Rows[i].Cells[3].Value + "','" + date + "','" + month + "','" + attendance + "','" + year + "')", con);
cmd.ExecuteNonQuery();
}