[英]C# Insert data from dataset into datagridview
I have 3 rows of data in the database. 我在数据库中有3行数据。 I loaded these data into a dataset first but failed to load these 3 rows into dgvCustListing.
我首先将这些数据加载到数据集中,但未能将这3行加载到dgvCustListing中。 It only loads the last data with 3 blank space on top.
它只加载最后一个数据,顶部有3个空格。 Could anyone please point my mistakes?
有人可以指出我的错误吗?
Thank you. 谢谢。
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();
}
}
}
I think you should use 我认为你应该使用
dgvCustListing.Rows[rowcount - 1] = ..... dgvCustListing.Rows [rowcount-1] = .....
.. I think .. 我认为
Instead for iterating the collection and adding row by row, Why not use binding like the following? 为什么不像下面那样使用绑定来迭代集合并逐行添加呢?
dgvCustListing.AutoGenerateColumns = true;
dgvCustListing.DataSource = DSCust_Listing; // is your dataset
dgvCustListing.DataMember = "Cust_Listing"; // is your table name to bind
Since you have only one query to execute, you need not to use a DataSet, instead for that you can use the adapter to populate the result to a DataTable. 由于只有一个查询要执行,因此您不必使用数据集,而是可以使用适配器将结果填充到数据表中。 Let
DTCust_Listing
be that datatable, then you can assign that as DataSource
for the dgvCustListing
假设
DTCust_Listing
是该数据表,则可以将其分配为dgvCustListing
DataSource
。
I don't think you need a DataSet. 我认为您不需要数据集。 A dataSet is an in memory representation of a collection of tables.
数据集是表集合在内存中的表示。 You can go for a DataTable instead.
您可以改为使用DataTable。
declare a Data Table 声明数据表
DataTable DSCust_Listing = new DataTable();
Instead of looping through the DataSet, just do 不必遍历数据集,而只需执行
if(DSCust_Listing != null)
{
dgvCustListing.DataSource = DSCust_Listing;
dgvCustListing.DataBind();
}
That should do it. 那应该做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.