我在數據庫中有3行數據。我先將這些數據加載到數據集中,但未能將這3行加載到dgvCustListing中。它只在最上面加載3個空格的數據。任何人都可以請指出我的錯誤?C#將數據集中的數據插入到datagridview中
謝謝。
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();
}
}
}
它的工作!非常感謝你!! – choi
不客氣! – user5328504
如果確實可以幫助您,您可以將其標記爲已接受 –