如何将数据表中的条件数据添加到另一个数据表中。 [错误:“位置0没有行] .C#

时间:2018-04-25 12:29:26

标签: c# datagridview datatable rows dbf

我是编程新手,并且创建了一个将.DBF表转换为.csv文件的工具。

所以这是场景; dbf表'Poles'包含四个字段'pole_id','guy_hoa_1','guy_hoa_2','guy_hoa_3'和'guy_hoa_4'。 并且最终的csv文件应该仅显示两列中的值:'PoleId'和'HOA',其中PoleID将是== pole_id而HOA = guy_hoa_1 +'|' + guy_hoa_2 + '|' + guy_hoa_3 +'|'+ guy_hoa_4。

例如,Poles表将具有类似的数据; Sample data of Poles table

并且,输出csv文件应显示如下数据;

Sample Output CSV file

* pole_id是主要字段,并根据它选择其他字段的值。

到目前为止,我设法编写以下代码:

在这里输入代码

enter code here
            string str = textBox1.Text;
            string path = str.Substring(0, str.LastIndexOf("\\") + 1);

            string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + path + "';Extended Properties=dBase IV;User ID=Admin;Password=;";
            OleDbConnection connection = new OleDbConnection();
            connection.ConnectionString = conn;
            connection.Open();
            CheckConnectionLabel.Text = "Connected Successfully";



            OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT pole_id, guy_hoa_1, guy_hoa_2,guy_hoa_3,guy_hoa_4 FROM poles" + ".dbf", connection);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            adapter.Fill(dt);


         DataTable dt1 = dt.AsEnumerable()
                        .Where(r=> r.Field<string>("pole_id")!= null)
                        .Where(r=> r.Field<string>("pole_id")!=" ")
                        .CopyToDataTable();




         DataTable dtTemp = new DataTable();
         dtTemp.Columns.Add("PoleId", typeof(String)); 
         dtTemp.Columns.Add("HOA", typeof(string));
         string x = string.Empty;



        for (int i=0;i< dt1.Rows.Count;i++)
        {

            if(dt1.Rows[i]["pole_id"]!= null || dt1.Rows[i]["pole_id"].ToString()!= "")

          {

              if(dt1.Rows[i]["guy_hoa_1"]!=null && dt1.Rows[i]["guy_hoa_1"].ToString()!="")

                { 

                     x =dt1.Rows[i]["guy_hoa_1"].ToString();
                }

             if(dt1.Rows[i]["guy_hoa_2"]!= null && dt1.Rows[i]["guy_hoa_2"].ToString()!="")

               {
                      x = x + "|" + dt1.Rows[i]["guy_hoa_2"].ToString();
               }


            if(dt1.Rows[i]["guy_hoa_3"]!=null && dt1.Rows[i]["guy_hoa_3"].ToString()!= "")
              {
                       x = x + "|" + dt1.Rows[i]["guy_hoa_3"].ToString();
              }

             if(dt1.Rows[i]["guy_hoa_4"]!=null && dt1.Rows[i]["guy_hoa_4"].ToString()!= "")
             {
                       x = x + "|" + dt1.Rows[i]["guy_hoa_4"].ToString();
             }


           dtTemp.Rows[i]["PoleId"] = dt1.Rows[i]["poles_id"].ToString();
           dtTemp.Rows[i]["HOA"] = x ;

          }

          }

       connection.Close();


       dataGridView1.DataSource = dtTemp;



       }
        catch (Exception ex)
              {
                    MessageBox.Show("Error " + ex.Message);
               }
 }


enter code here

因此,通过上面的代码,我连接到dbf表并在'dt'表中收集了所需的数据。然后我通过删除pole_id为空/ null的行来过滤数据,并将其放在另一个'dt1'表中。现在我的目的是检查dt1表中的条件,然后填充dtTemp表中的行,稍后将在datagridview中显示数据。

代码正在获取x的值直到最后一个IF语句,但是没有任何东西在dtTemp数据表中填满,然后显示此错误。

请帮助我,让我知道我错在哪里......非常感谢提前!!

1 个答案:

答案 0 :(得分:0)

我得到的解决方案如下;

    if result.truncatingRemainder(dividingBy: 1) == 0{
    screenLabel.text = String(Int(result))
    }
    else{
    screenLabel.text = String(result)
    }
相关问题