我在数据库中有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();
}
}
}
答案 0 :(得分:0)
我认为你应该使用
dgvCustListing.Rows [rowcount - 1] = .....
..我想
答案 1 :(得分:0)
而不是迭代集合并逐行添加,为什么不使用如下的绑定?
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
答案 2 :(得分:0)
我认为你不需要DataSet。 dataSet是表集合的内存表示。你可以改为使用DataTable。
声明数据表
DataTable DSCust_Listing = new DataTable();
而不是循环遍历DataSet,只需执行
if(DSCust_Listing != null)
{
dgvCustListing.DataSource = DSCust_Listing;
dgvCustListing.DataBind();
}
应该这样做。