I have a class:
public class MarketTrade
{
public int trade_seq;
public double amount;
public double price;
public direction dir; //=enum
}
I want my dataGridView1 show table like:
trade_seq amount price dir
00001 10 100 buy
00002 5 99 buy
00003 5 100 buy
00004 15 98 sell
00005 20 100 sell
I try this but failed:
MarketTrade[] trades = GetTrades();
this.dataGridView1.DataSource = trades;
Is there a simple way to do it?
Create a class with properties as follows:
public class MarketTrade
{
public int TradeSeq { get; set; }
public double Amount { get; set; }
public double Price { get; set; }
public Direction Dir { get; set; }
}
Create an enum as follows:
public enum Direction
{
Buy,
Sell
}
Create your data source (I have created a random Data Source with the help of this method for 10 objects):
private MarketTrade[] GetTrades()
{
MarketTrade[] arrMarketTrades = new MarketTrade[10];
for (int i = 0; i < 10; i++)
{
arrMarketTrades[i] = new MarketTrade()
{
Amount = (i + 1) * 4,
Dir = i / 2 == 0 ? Direction.Buy : Direction.Sell,
Price = (i + 1) * 2,
TradeSeq = i
};
}
return arrMarketTrades;
}
Finally, setting the data source in form load event, you can change it as per your needs:
private void Form1_Load(object sender, System.EventArgs e)
{
dataGridView1.DataSource = this.GetTrades();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.