【问题标题】:C# Insert data from dataset into datagridviewC#将数据集中的数据插入datagridview
【发布时间】:2017-04-26 03:41:03
【问题描述】:

我在数据库中有 3 行数据。我首先将这些数据加载到数据集中,但未能将这 3 行加载到 dgvCustListing。它只加载顶部有 3 个空格的最后一个数据。谁能指出我的错误?

谢谢。

private void LoadCustListing()
    {
        string sQuery = string.Empty;
        int rowcount = 0;
        dgvCustListing.Rows.Clear();
        string spartyConn = ProcessData.GetpartyConnStr();
        using (SqlConnection Conn = new SqlConnection(spartyConn))
        {
            try
            {
                Conn.Open();
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                Cmd.CommandTimeout = 1000;


                sQuery = "select pd.Party_Id, pd.Outlet_StoreNo, 'storename', pd.Party_Date, 'time', bc.Child_Name, pd.Party_Status "
                + "from Party_Dtl pd inner join Bday_Child bc "
                + "on pd.Child_Id = bc.Child_Id "
                + "inner join Cust_Dtl cd "
                + "on bc.Parent_Id = cd.Parent_Id "
                + "where pd.Party_Status = 'open' ";

                Cmd.CommandText = sQuery.ToString();
                SqlDataAdapter ExportAdapter = new SqlDataAdapter(sQuery, Conn);
                ExportAdapter.SelectCommand.CommandTimeout = 10000;
                DataSet DSCust_Listing = new DataSet();
                ExportAdapter.Fill(DSCust_Listing, "Cust_Listing");

                foreach (DataRow DSRow in DSCust_Listing.Tables["Cust_Listing"].Rows)
                {
                    rowcount += 1;
                    dgvCustListing.Rows.Add();
                    dgvCustListing.Rows[dgvCustListing.RowCount - 1].Cells[0].Value = rowcount;
                    dgvCustListing.Rows[dgvCustListing.RowCount - 1].Cells[2].Value = DSRow["Outlet_StoreNo"];
                    dgvCustListing.Rows[dgvCustListing.RowCount - 1].Cells[3].Value = "storename";//DSRow["Outlet"];
                    dgvCustListing.Rows[dgvCustListing.RowCount - 1].Cells[4].Value = DateTime.Parse(DSRow["Party_Date"].ToString());
                    dgvCustListing.Rows[dgvCustListing.RowCount - 1].Cells[5].Value = "time";//DSRow["Party_Time"]; //Time + AMPM
                    dgvCustListing.Rows[dgvCustListing.RowCount - 1].Cells[6].Value = DSRow["Child_Name"];
                    dgvCustListing.Rows[dgvCustListing.RowCount - 1].Cells[7].Value = DSRow["Party_Status"];
                }
            }
            catch (Exception ex)
            {
                if (ex.Message != "Exception from HRESULT: 0x800A03EC")
                {

                }
            }
            finally
            {
                Conn.Close();
            }
        }
    }

click for dgvCustListing image

【问题讨论】:

    标签: c# datagridview dataset


    【解决方案1】:

    我认为你应该使用

    dgvCustListing.Rows[rowcount - 1] = .....

    .. 我认为

    【讨论】:

      【解决方案2】:

      我认为您不需要数据集。数据集是表集合的内存表示。您可以改为使用 DataTable。

      声明一个数据表

      DataTable DSCust_Listing = new DataTable();
      

      不要循环遍历数据集,而是这样做

      if(DSCust_Listing != null)
      {
          dgvCustListing.DataSource = DSCust_Listing;
          dgvCustListing.DataBind();
      }
      

      应该可以的。

      【讨论】:

      • 嗨,我试过了,这个错误出来了:错误 19 'System.Windows.Forms.DataGridView' 不包含 'DataBind' 的定义,并且没有扩展方法 'DataBind' 接受第一个参数可以找到类型“System.Windows.Forms.DataGridView”(您是否缺少 using 指令或程序集引用?)
      【解决方案3】:

      而不是迭代集合并逐行添加,为什么不使用如下绑定?

      dgvCustListing.AutoGenerateColumns = true;
      dgvCustListing.DataSource = DSCust_Listing; // is your dataset
      dgvCustListing.DataMember = "Cust_Listing"; // is your table name to bind
      

      由于您只有一个要执行的查询,因此您无需使用 DataSet,而是可以使用适配器将结果填充到 DataTable。让DTCust_Listing 成为该数据表,然后您可以将其分配为DataSource 用于dgvCustListing

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-06
        • 2020-07-05
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 2012-11-24
        相关资源
        最近更新 更多