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");
}
}
这是我的代码。首先,我从1个employee_details表的3列填写gridview。同样的列也出现在考勤表中。现在,当我点击按钮时,这三列将按原样放入考勤表,其余列从各种控件中取出
答案 0 :(得分:0)
你使用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]”而不是“出勤”。
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);
答案 1 :(得分:0)
或者您可以在写入数据库的循环内移动字符串逻辑: (并完全摆脱与你一起工作的另一个循环&#34;现在&#34;以及&#34;缺席&#34;)
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();
}