-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
115 lines (93 loc) · 3.38 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private Dictionary<string, string> vehicles = new Dictionary<string, string>
{
{ "train", "rails" },
{ "airplane", "jet engine" },
{ "car", "wheels" }
// Add more vehicles as needed
};
public Form1()
{
InitializeComponent();
InitializeDataGridView();
FillGrid();
// Add this line to subscribe to the RowPostPaint event
dataGridView1.RowPostPaint += DataGridView1_RowPostPaint;
}
private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
if (row.DataBoundItem is Players player)
{
// Check if the player object has an image and it's not null
if (player.Image != null)
{
int imageHeight = player.Image.Height;
if (imageHeight > row.Height)
{
row.Height = imageHeight;
}
}
}
}
private void InitializeDataGridView()
{
dataGridView1.AutoGenerateColumns = true;
}
public class Players
{
public string Vehicle { get; set; }
public string Method { get; set; }
public Image Image { get; set; } // Add this property
}
private void FillGrid()
{
try
{
var playersList = new List<Players>();
foreach (var vehicle in vehicles)
{
Players p = new Players
{
Vehicle = vehicle.Key,
Method = vehicle.Value
};
p.Image = Image.FromFile($"Images\\{vehicle.Key.ToLower()}.png");
playersList.Add(p);
}
dataGridView1.DataSource = playersList;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
FillGrid();
}
private int _previousIndex;
private bool _sortDirection;
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == _previousIndex)
_sortDirection ^= true; // toggle direction
dataGridView1.DataSource = SortData((List<Players>)dataGridView1.DataSource, dataGridView1.Columns[e.ColumnIndex].Name, _sortDirection);
_previousIndex = e.ColumnIndex;
}
public List<Players> SortData(List<Players> list, string column, bool ascending)
{
return ascending ?
list.OrderBy(_ => _.GetType().GetProperty(column).GetValue(_)).ToList() :
list.OrderByDescending(_ => _.GetType().GetProperty(column).GetValue(_)).ToList();
}
}
}