我正在使用win-forms。我有一个datagridview,它从2个SQL数据库表中加载数据。我想将数据加载到datagridview,因为数据只显示一次。
此处dtp
是datetimepicker,我想要的查询s_no
应来自dailyattendance
(稍后我将s_no
值递增,就像表格中的最后S_no
为10一样,将其值增加到
11,12,13
等等,同时将数据加载到datagridview)表并保留
列应来自员工详细信息
当我使用下面的代码时,datagridview与许多声誉绑定,我不想要
private void bindgrid()
{
try
{
dataGridView1.ColumnCount = 10;
ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
string connectionString = consettings.ConnectionString;
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
string dtp = dateTimePicker3grd.Value.ToString("dd/MM/yyyy");
//in this query if use ON d.Employee_Id = e.Employee_Id query
// it is showing only equal values with no reputation,so i tried to ON d.Employee_Id != e.Employee_Id query then many reputations are occurring.I dont want this
string query = "SELECT d.S_No,e.Employee_id,e.Employee_name,e.image_of_employee
FROM dailyattendance d JOIN employee_details e
ON d.Employee_Id = e.Employee_Id where e.Employee_Id not in (select Employee_Id from dailyattendance where date = '" + dtp + "' ) ";
SqlCommand cmd = new SqlCommand(query, cn);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
int maxSlNo = int.Parse(dt.Rows[dt.Rows.Count - 1]["S_No"].ToString());
maxSlNo++;
foreach (DataRow dtRow in dt.Rows)
{
dtRow["S_No"] = maxSlNo;
maxSlNo++;
}
dt.AcceptChanges();
dataGridView1.Columns[0].Name = "S_No";
dataGridView1.Columns[0].HeaderText = "S_No";
dataGridView1.Columns[0].DataPropertyName = "S_No";
dataGridView1.Columns[1].HeaderText = "Employee_id";
dataGridView1.Columns[1].Name = "Employee_Id";
dataGridView1.Columns[1].DataPropertyName = "Employee_id";
dataGridView1.Columns[2].Name = "Employee_name";
dataGridView1.Columns[2].HeaderText = "Employee_Name";
dataGridView1.Columns[2].DataPropertyName = "Employee_name";
dataGridView1.Columns[3].Name = "In_time";
dataGridView1.Columns[3].HeaderText = "In_time";
dataGridView1.Columns[4].Name = "Out_time";
dataGridView1.Columns[4].HeaderText = "Out_time";
dataGridView1.Columns[5].Name = "Date";
dataGridView1.Columns[5].HeaderText = "Date";
dataGridView1.Columns[6].Name = "Week_no_of_the_Month";
dataGridView1.Columns[6].HeaderText = "Week_no_of_the_Month";
dataGridView1.Columns[7].HeaderText = "Attendance";
dataGridView1.Columns[7].Name = "Attendance";
dataGridView1.Columns[8].Name = "Work_status";
dataGridView1.Columns[8].HeaderText = "Work_status";
dataGridView1.Columns[9].Name = "Remarks";
dataGridView1.Columns[9].HeaderText = "Remarks";
// for Image_Of_employee I did not give column but automatically displaying in datagridview last column's cells
dataGridView1.DataSource = dt;
}
}
}
}
catch(Exception e1)
{
MessageBox.Show(e1.Message);
}
}
我在表单load()
中调用了这个方法
答案 0 :(得分:1)
替换
string query = "SELECT d.S_No,e.Employee_id,e.Employee_name,e.image_of_Employee
FROM dailyattendance as d,employee_details AS e where e.Employee_Id
not in (select Employee_Id from dailyattendance where date = '" + dtp + "' )
Order By d.S_No";
带
string query = "SELECT DISTINCT d.S_No,e.Employee_id,e.Employee_name,e.image_of_Employee
FROM dailyattendance d JOIN employee_details e ON d.Employee_Id = e.Employee_Id
not in (select Employee_Id from dailyattendance where date = '" + dtp + "' )
Order By d.S_No";
答案 1 :(得分:0)
您从2个表中选择数据,但未连接表。您应该从
更改查询部分FROM dailyattendance as d,employee_details AS e
到
FROM dailyattendance d JOIN employee_details e ON d.Employee_Id = e.Employee_Id