Foreach使用C#和JSON将项绑定到Datatable(Foreach bind items to Datatable with C# and JSON)

编程入门 行业动态 更新时间:2024-10-22 20:20:06
Foreach使用C#和JSON将项绑定到Datatable(Foreach bind items to Datatable with C# and JSON) DataTable dtInventory = new DataTable(); dtInventory.Columns.Add("ItemID", typeof(string)); dtInventory.Columns.Add("ItemImageUrl", typeof(string)); dtInventory.Columns.Add("ItemName", typeof(string)); dtInventory.Columns.Add("Tradeable", typeof(string)); string json = new WebClient().DownloadString("storeJSON.txt"); JToken jsonInventory = JToken.Parse(json); JObject jsonItemData = jsonInventory["StoreData"].Value<JObject>(); foreach (JProperty jItemID in jsonItemData.Properties()) { string sItemID = jItemID.Name.Trim().ToString(); for (int i = 0; i < jsonItemData.Count; i++) { DataRow dtRow = dtInventory.NewRow(); dtRow["ItemID"] = sItemID.ToString(); dtRow["ItemImageUrl"] = jsonItemData[sItemID]["icon_url"].ToString(); dtRow["ItemName"] = jsonItemData[sItemID]["market_name"].ToString(); dtRow["Tradeable"] = jsonItemData[sItemID]["tradable"].ToString(); } } this.rptInventory.DataSource = dtInventory; this.rptInventory.DataBind();

我是c#和asp.net的新手,我花了2个小时来修复这些代码,但我仍然无法将项目绑定到DataTable。 请帮助我。

DataTable dtInventory = new DataTable(); dtInventory.Columns.Add("ItemID", typeof(string)); dtInventory.Columns.Add("ItemImageUrl", typeof(string)); dtInventory.Columns.Add("ItemName", typeof(string)); dtInventory.Columns.Add("Tradeable", typeof(string)); string json = new WebClient().DownloadString("storeJSON.txt"); JToken jsonInventory = JToken.Parse(json); JObject jsonItemData = jsonInventory["StoreData"].Value<JObject>(); foreach (JProperty jItemID in jsonItemData.Properties()) { string sItemID = jItemID.Name.Trim().ToString(); for (int i = 0; i < jsonItemData.Count; i++) { DataRow dtRow = dtInventory.NewRow(); dtRow["ItemID"] = sItemID.ToString(); dtRow["ItemImageUrl"] = jsonItemData[sItemID]["icon_url"].ToString(); dtRow["ItemName"] = jsonItemData[sItemID]["market_name"].ToString(); dtRow["Tradeable"] = jsonItemData[sItemID]["tradable"].ToString(); } } this.rptInventory.DataSource = dtInventory; this.rptInventory.DataBind();

I'm newbie to c# and asp.net and it took me 2 hours to fix these codes but I still can't bind items to DataTable. Please help me out.

最满意答案

看起来你没有将行添加到表中,所以当绑定发生时,没有数据。 这应该可以解决这个问题:

for (int i = 0; i < jsonItemData.Count; i++) { DataRow dtRow = dtInventory.NewRow(); dtRow["ItemID"] = sItemID.ToString(); dtRow["ItemImageUrl"] = jsonItemData[sItemID]["icon_url"].ToString(); dtRow["ItemName"] = jsonItemData[sItemID]["market_name"].ToString(); dtRow["Tradeable"] = jsonItemData[sItemID]["tradable"].ToString(); dtInventory.Rows.Add(dtRow); //this line adds the row to the table }

It looks like you're not adding the rows to the table so when the binding takes place, there's no data. This should fix that problem:

for (int i = 0; i < jsonItemData.Count; i++) { DataRow dtRow = dtInventory.NewRow(); dtRow["ItemID"] = sItemID.ToString(); dtRow["ItemImageUrl"] = jsonItemData[sItemID]["icon_url"].ToString(); dtRow["ItemName"] = jsonItemData[sItemID]["market_name"].ToString(); dtRow["Tradeable"] = jsonItemData[sItemID]["tradable"].ToString(); dtInventory.Rows.Add(dtRow); //this line adds the row to the table }

更多推荐

本文发布于:2023-07-26 00:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   JSON   Foreach   items   bind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!