窗口表单应用程序使用datagridview参加员工 [英] Window Form Application to Take A attendance of Employee using datagridview
本文介绍了窗口表单应用程序使用datagridview参加员工的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
////////////////////////////////dataGridview not generated
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ROEAS
{
public partial class Attendance : Form
{
SqlDataAdapter dap;
DataTable dt;
string a;//Here shows a error
string b;
public Attendance()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// d1.ConOpen();
// dt = d1.DisplayGrid("select emp_code,name,department from employee");
dataGridView1.DataSource = dt;
// d1.ConClose();
generate();
//data();
int t;
for (t = 0; t <= dataGridView1.Columns.Count - 1; t++)
{
dataGridView1.Columns[t].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[t].ReadOnly = true;
dataGridView1.Columns[t].DefaultCellStyle.BackColor = Color.White;
}
data();
}
private void Button3_Click(object sender, EventArgs e)
{
// d1.ConOpen();
int t;
DateTime d;
d = DateTime.Parse(a);
string s = d.Month + "-" + d.Day + "-" + d.Year;
for (t = 0; t <= dataGridView1.Rows.Count - 1; t++)
{
//d1.Delete("delete from attendence where no=" + dataGridView1.Rows[t].Cells["emp_code"].Value.ToString() + " and convert(varchar,d,101)='" + d.ToString() + "'");
}
for (t = 0; t <= dataGridView1.Rows.Count - 1; t++)
{
// d1.Insert("Insert into attendance (att_date,emp_code,is_present) values('" + s + "'," + dataGridView1.Rows[t].Cells["emp_code"].Value.ToString() + "," + "1" + ")");
}
MessageBox.Show("Complate Fill Attendence");
// d1.ConClose();
}
private void button1_Click(object sender, EventArgs e)
{
}
public void data()
{
//d1.ConOpen();
int t;
int u;
//DateTime dt1 ;
string dt1 = "";
for (u = 3; u <= dataGridView1.Columns.Count - 5; u++)
{
dt1 = DateTime.Parse(dataGridView1.Columns[u].HeaderText).ToShortDateString();
DataTable dt = new DataTable();
// dt = d1.DisplayGrid("select * from attendence where convert(varchar,d,101)='" + dt1.ToString() + "'");
if (dt.Rows.Count > 0)
{
for (t = 0; t < dt.Columns.Count; t++)
{
if (dt.Rows[t]["status"].ToString() != "True")
{
dataGridView1.Rows[t].Cells[u].Value = true;
}
else
{
dataGridView1.Rows[t].Cells[u].Value = false;
}
}
}
}
//d1.ConClose();
//return 0;
}
public void generate()
{
DateTime i;
DateTime j;
int[] a = new int[4];
DataTable dt1=new DataTable();
// dt1 = d1.DisplayGrid("select convert(varchar,SDate,101) ,convert(varchar,EDate,101) from semester");
i = DateTime.Parse(dt1.Rows[0][0].ToString());
j = DateTime.Parse(dt1.Rows[0][1].ToString());
while (i.CompareTo(j) != 0)
{
DataGridViewCheckBoxColumn dg = new DataGridViewCheckBoxColumn();
dg.HeaderText = i.Day + "/" + i.Month + "/" + i.Year;
dg.Width = 30;
dataGridView1.Columns.Add(dg);
i = i.Date.AddDays(1);
}
dataGridView1.Columns.Add("Total", "Total");
}
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
int t;
for (t = 0; t <= 1; t++)
{
dataGridView1.Columns[t].Frozen = true;
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int i;
int u;
int k;
for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
k = 0;
for (u = 3; u <= dataGridView1.Columns.Count - 5; u++)
{
if (dataGridView1.Rows[i].Cells[u].Value != null)
{
k = k + 1;
}
}
if (k == 0)
{
dataGridView1.Rows[i].Cells["Total"].Value = 0;
}
else
{
dataGridView1.Rows[i].Cells["Total"].Value = k;
}
}
}
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
a = dataGridView1.Columns[e.ColumnIndex].HeaderText;
b = e.ColumnIndex.ToString();
int t;
for (t = 0; t <= dataGridView1.Columns.Count - 2; t++)
{
if (dataGridView1.Columns[t].HeaderText == dataGridView1.Columns[e.ColumnIndex].HeaderText)
{
dataGridView1.Columns[t].ReadOnly = false;
dataGridView1.Columns[t].DefaultCellStyle.BackColor = Color.DarkTurquoise;
}
else
{
dataGridView1.Columns[t].ReadOnly = true;
dataGridView1.Columns[t].DefaultCellStyle.BackColor = Color.White;
}
}
}
private void ChkAll_CheckedChanged(object sender, EventArgs e)
{
if ((ChkAll.Checked))
{
int t;
for (t = 3; t <= dataGridView1.Columns.Count - 5; t++)
{
string g = dataGridView1.Columns[t].HeaderText.ToString();
DateTime d;
d = DateTime.Parse(dataGridView1.Columns[t].HeaderText);
if (g.CompareTo(a) == 0)
{
int i;
for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[t].Value = true;
}
}
}
}
else
{
int t;
for (t = 3; t <= dataGridView1.Columns.Count - 5; t++)
{
DateTime d;
d = DateTime.Parse(dataGridView1.Columns[t].HeaderText);
string g = dataGridView1.Columns[t].HeaderText.ToString();
if (g.CompareTo(a) == 0)
{
int i;
for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[t].Value = false;
}
}
}
}
}
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Attendance_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'logindbDataSet9.Attendance' table. You can move, or remove it, as needed.
this.attendanceTableAdapter.Fill(this.logindbDataSet9.Attendance);
} }
}
推荐答案
首先,您将a声明为字符串。
然后你写下面的代码将它解析为DateTime。
Firstly you are declaring "a" as string.
Then you are writing below code to Parse it into DateTime.
d = DateTime.Parse(a);
你需要在解析它之前进行初始化。
You need to initialize before you parse it.
这篇关于窗口表单应用程序使用datagridview参加员工的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文