簡體   English   中英

如何在C#中使用Windows窗體打印對象

[英]how can I print an object using Windows Forms in C#

我收到有關嘗試將null推入字段的錯誤,這是我使用的表中的外鍵。 所以我更改了它,但是我不確定是否抓住了正確的對象,所以我想打印我剛剛推送的數據庫表。 我知道我可以查詢該表,但是鑒於我只看到id,而且幾乎是午夜,所以我可以打印並確保它是我在表單中創建的值,感覺更好。 這是代碼...

foreach (TeamModel tm in model.EnteredTeams)
{
     var p = new DynamicParameters();
     p.Add("@TournamentId", model.Id);
     //p.Add("@PrizeId", tm.Id);
     //p.Add("@PrizeId", model.Prizes);
     p.Add("@PrizeId", model.Prizes.First().Id);
     p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
     connection.Execute("spTournamentPrizes_Insert", p, commandType: CommandType.StoredProcedure);
     MessageBox.Show("This is the prize you just pushed to the Database." , model.Prizes.First());

 }

如您所見,我正在嘗試使用MessageBox,但顯然僅根據我閱讀的文檔,它才用於字符串。 如何打印獎品?

這是我會做的:

編寫將獎賞對象轉換為獎賞類中的字符串的方法,例如:

public string GetPrizeDetailsAsString()
{
return "ID:"+Id.ToString()+..... // u can add all the params from prize which u wanna view;
}

然后調用model.Prize.First()。GetPrizeDetailAsString()將為您提供完整的詳細信息。

例如:

 MessageBox.Show("This is the prize you just pushed to the Database." , model.Prizes.First().GetPrizeDetailAsString());

您可以編寫方法以從獎賞對象返回值。

public string ConvertPrizeToString(Prize prize)
{
    return string.Format("Id:{0}, Other Details:{1}", prize.Id, prize.OtherDetails);
}

然后您可以在MessageBox中調用該函數

MessageBox.Show("This is the prize you just pushed to the Database.", ConvertPrizeToString(model.Prizes.First()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM