我正在使用此代码
private void button1_Click(object sender, EventArgs e)
{
int cnt = 0;
DataTable dataTable3 = null;
DataTable dataTable1 = null, dataTable2 = null;
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
{
string empcode = dataGridView1.Rows[i].Cells[1].Value.ToString();
string mnth = dtpicker.Value.Month.ToString();
dataTable1 = Salaryslip.DataTable1(empcode,mnth );
dataTable2 = Salaryslip.DataTable2(empcode, mnth);
string str = dataTable1.Columns["empid"].ToString();
if (cnt == 0)
{
dataTable1.PrimaryKey = new DataColumn[] { dataTable1.Columns["empid"] };
dataTable2.PrimaryKey = new DataColumn[] { dataTable2.Columns["empid"] };
} dataTable3 = dataTable1.Copy();
dataTable3.Merge(dataTable2, false, MissingSchemaAction.Add);
dataTable3.AcceptChanges();
cnt = cnt + 1;
double total = 0, tot = 1;
for (int j = 0; j <= dataTable3.Rows.Count - 1;j++)
{
if (dataTable3.Rows[j][2].ToString() != "")
{
total += Convert.ToDouble(dataTable3.Rows[j][2].ToString());
}
if (dataTable3.Rows[j][9].ToString() != "")
{
string str1 = dataTable3.Rows[j][10].ToString();
tot += Convert.ToDouble(dataTable3.Rows[j][10].ToString());
}
}
//DataRow dr;
//dr = dataTable3.NewRow();
//cnt = cnt + 1;
//// dr[0] = cnt;
//dr[1] = "Total Earning";
//dr[2] = total;
//dr[9] = "Total Deductions";
//dr[10] = tot;
//dataTable3.Rows.Add(dr);
//dr = dataTable3.NewRow();
////dr[0] = cnt+1;
//dr[9] = "Net Payable Amount";
//dr[10] = total - tot;
//dataTable3.Rows.Add(dr);
double amt = ( total - tot);
string abc=Convert.ToString(amt);
string std=(Strings.Mid(abc,1,1));
string wrds=null;
if(std!="-")
{
wrds = CommonFunctions.ConvertNumberInWords(amt);
}
else
{
// MessageBox.Show("Amount in negative","Mind Excel");
}
DisplayReport(dataTable3, wrds, Convert.ToString(total), Convert.ToString(tot), Convert.ToString(amt));
}
}
}
public static DataTable DataTable1( string emcode,string mnth)
{
DataRow dataRow = null;
DataTable dt1 = new DataTable();
DatabaseConnection.setupDB();
string qur = "SELECT Employees.EmployeeCode, Employees.EmployeeName, Departments.DepartmentFName, tblsalcal.mnths, tblsalcal.years, tblsalcal.heads, tblsalcal.sal,Companies.companyfname,Companies.CompanyAddress FROM (((tblsalcal INNER JOIN Employees ON tblsalcal.empcode = Employees.EmployeeCode) INNER JOIN Departments ON Employees.DepartmentId = Departments.DepartmentId) inner join Companies on Companies.CompanyId=Employees.CompanyId) inner join tbl_heads on tbl_heads.head_name=tblsalcal.heads where employees.employeecode ='"+ emcode +"' and tblsalcal.mnths='" + mnth +"' and tbl_heads.type='e' order by employees.employeecode, tbl_heads.id ";
DataSet ds = new DataSet();
OleDbCommand oledbcmd = new OleDbCommand(qur);
OleDbDataReader rdr = DatabaseConnection.MyExecuteReader(oledbcmd);
int cont = 0;
double total = 0;
var idColumn = new DataColumn("EmpId", typeof(System.Int32));
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 0;
idColumn.AutoIncrementStep = 1;
dt1.Columns.Add(idColumn);
dt1.PrimaryKey = new DataColumn[] { idColumn };
dt1.Columns.Add("heads");
dt1.Columns.Add("sal");
dt1.Columns.Add("employeecode");
dt1.Columns.Add("employeename");
dt1.Columns.Add("departmentfname");
dt1.Columns.Add("company");
dt1.Columns.Add("comad");
dt1.Columns.Add("mnths");
// dt1.Columns.Add("years");
// dt1.Columns.Add("employeecode");
while (rdr.Read())
{
if (cont == 0)
{
dataRow = dt1.NewRow();
//dataRow["empid"] = rdr[0].ToString();
string str = rdr[0].ToString();
dataRow[1] = rdr["heads"].ToString();
total=total+ Convert.ToDouble(rdr["sal"].ToString());
dataRow[2] = rdr["sal"].ToString();
dataRow[3] = rdr["employeecode"].ToString();
dataRow[4] = rdr["employeename"].ToString();
dataRow[5] = rdr["departmentfname"].ToString();
dataRow[6] = rdr["companyfname"].ToString();
dataRow[7] = rdr["CompanyAddress"].ToString();
str=swithcasem(Convert.ToInt16(rdr["mnths"].ToString()));
dataRow[8] = "for" + " " + str + " " + "-"+ rdr["years"].ToString();
// dataRow[9] = rdr["years"].ToString();
dt1.Rows.Add(dataRow);
cont = cont + 1;
}
else
{
dataRow = dt1.NewRow();
// dataRow["empid"] = cont;
string str = rdr[0].ToString();
dataRow[1] = rdr["heads"].ToString();
dataRow[2] = rdr["sal"].ToString();
total = total + Convert.ToDouble(rdr["sal"].ToString());
dt1.Rows.Add(dataRow);
cont = cont + 1;
}
}
dt1.AcceptChanges();
DatabaseConnection.UnloadDB(false);
//dataRow = dt1.NewRow();
//dataRow["empid"] = cont;
//dataRow[1] = "Total Earnings";
//dataRow[2] = total;
//dt1.Rows.Add(dataRow);
//cont = cont + 1;
return dt1;
}
public static DataTable DataTable2( string emcode,string mnth)
{
DataRow dr = null;
DataTable dt2 = new DataTable();
DatabaseConnection.setupDB();
string qur = "SELECT Employees.EmployeeCode, Employees.EmployeeName, Departments.DepartmentFName, tblsalcal.mnths, tblsalcal.years, tblsalcal.heads, tblsalcal.sal,Companies.companyfname,Companies.CompanyAddress FROM (((tblsalcal INNER JOIN Employees ON tblsalcal.empcode = Employees.EmployeeCode) INNER JOIN Departments ON Employees.DepartmentId = Departments.DepartmentId) inner join Companies on Companies.CompanyId=Employees.CompanyId) inner join tbl_heads on tbl_heads.head_name=tblsalcal.heads where employees.employeecode ='" + emcode + "' and tblsalcal.mnths='" + mnth + "' and tbl_heads.type='d' order by employees.employeecode, tbl_heads.id ";
DataSet ds = new DataSet();
OleDbCommand oledbcmd = new OleDbCommand(qur);
OleDbDataReader rdr = DatabaseConnection.MyExecuteReader(oledbcmd);
var idColumn = new DataColumn("EmpId", typeof(System.Int32 ));
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 0;
idColumn.AutoIncrementStep = 1;
dt2.Columns.Add(idColumn);
dt2.PrimaryKey = new DataColumn[] { idColumn };
dt2.Columns.Add("Deduction");
int cont = 0;
double total = 0;
dt2.Columns.Add("amt");
while (rdr.Read())
{
if (cont == 0)
{
dr = dt2.NewRow();
string str = rdr[0].ToString();
// dr["empid"] = rdr[0].ToString();
dr[1] = rdr["heads"].ToString();
dr[2] = rdr["sal"].ToString();
total = total + Convert.ToDouble(rdr["sal"].ToString());
dt2.Rows.Add(dr);
cont = cont + 1;
}
else
{
dr = dt2.NewRow();
string str = rdr[0].ToString();
// dr["empid"] = cont;
dr[1] = rdr["heads"].ToString();
dr[2] = rdr["sal"].ToString();
total = total + Convert.ToDouble(rdr["sal"].ToString());
dt2.Rows.Add(dr);
cont = cont + 1;
}
}
//dr = dt2.NewRow();
//dr["empid"] = cont;
//dr[1] = "Total Deductions";
//dr[2] = total;
//dt2.Rows.Add(dr);
//cont = cont + 1;
return dt2;
}
但是当我选择多个雇主时,它只显示报告查看器中的最后一条记录。假设我选择1002和1003 empcode,但它只显示1003的记录 请帮帮我