【问题标题】:How to stop showing multiple times of data in datagridview while retrieving from database in c#如何在 c# 中从数据库中检索时停止在 datagridview 中显示多次数据
【发布时间】:2014-10-09 09:17:58
【问题描述】:

我正在使用 win-forms。我有一个 datagridview,它从 SQL 数据库的 2 个表中加载其数据,我想将数据加载到 datagridview,因为数据只显示一次。
这里dtp 是日期时间选择器,我想要的查询是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() 中调用了这个方法

【问题讨论】:

  • 你有没有在你的 sql 查询中使用 distinct
  • 你真的应该把所有的数据库东西放在至少一个单独的方法中,最好是一个单独的类,use a usings for the SqlConnection,避免using parameters的sql注入,...
  • 不,我没有在我的查询中使用 distinct 我应该将它放在查询@SmartDeveloper 的哪里
  • 从数据库中你得到的结果重复或在 datagridview 中你有问题,如果在 datagridview 上你需要设置 datagridview 的属性AllowUserToAddRows=false
  • 我使用了'AllowUserToAddRows=false',但问题仍然存在@Dotnet

标签: c# sql datagridview


【解决方案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";

【讨论】:

  • @ShivaDebrown 我不建议不参与,因为不参与会影响性能
  • @Shiva Debrown 请提及您遇到的错误
  • Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'not'. 抛出此错误。我的完整查询是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 = '02/10/2014' ) Order By d.S_No
  • @Shiva Debrown 请更新您的问题并添加您的表格结构,以便我可以从我身边检查它,如果可能的话
  • 请给我看表格字段名称和数据类型,而不是表格数据,我会像你的表格一样制作新表格,然后我可以说点什么......给我看表格脚本
【解决方案2】:

您从 2 个表中选择数据,但没有连接表。你应该改变你的查询部分

FROM dailyattendance as d,employee_details AS e

FROM dailyattendance d JOIN employee_details e ON d.Employee_Id = e.Employee_Id 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多