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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;
using ExcelObj = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApp3
{
public partial class Form2 : Form
{
string connectionString = @"Data Source=KVP-25793;Initial Catalog=STobjektuAtbildigie;Integrated Security=True;TransparentNetworkIPResolution=True";
public string sqlCon;
public Form2()
{
InitializeComponent();
dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;
}
bool IsTheSameCellValue(int column, int row)
{
DataGridViewCell cell1 = dataGridView1[column, row];
DataGridViewCell cell2 = dataGridView1[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
return cell1.Value.ToString() == cell2.Value.ToString();
}
public System.Data.DataTable LoadExcelSheetToTable16(string filename, string sheet)
{
System.Data.DataTable table16 = new System.Data.DataTable();
using (System.Data.OleDb.OleDbConnection co =
new System.Data.OleDb.OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0; " +
"Data Source='" + filename + "';" +
"Extended Properties=\"Excel 12.0;HDR=TES;IMEX=1\""))
using (System.Data.OleDb.OleDbDataAdapter import =
new System.Data.OleDb.OleDbDataAdapter(
"select * from [" + sheet + "$]", co))
import.Fill(table16);
return table16;
}
void PlaceTableToDatabase16(System.Data.DataTable table16)
{
Model2 db16 = new Model2();
table16.PrimaryKey = new DataColumn[] { table16.Columns["ID"] };
foreach (DataRow row in table16.Rows)
{
int ID = Convert.ToInt32(row["ID"]);
STatbPers STatbPers = db16.STatbPers.Find(ID);
if (STatbPers == null)
{
STatbPers = new STatbPers();
STatbPers.ID = ID;
STatbPers.Reģions = Convert.ToString(row["Reģions"]);
STatbPers.Objekts = Convert.ToString(row["Objekts"]);
STatbPers.Ēka = Convert.ToString(row["Ēka"]);
STatbPers.Personāls = Convert.ToString(row["Personāls"]);
STatbPers.Tālrunis = Convert.ToString(row["Tālrunis"]);
STatbPers.NĪAF = Convert.ToString(row["NĪAF"]);
STatbPers.Tālrunis1 = Convert.ToString(row["Tālrunis1"]);
STatbPers.Dispečerdienests = Convert.ToString(row["Dispečerdienests"]);
STatbPers.Tālrunis2 = Convert.ToString(row["Tālrunis2"]);
STatbPers.FDDDS = Convert.ToString(row["FDDDS"]);
STatbPers.Tālrunis3 = Convert.ToString(row["Tālrunis3"]);
STatbPers.Piezīmes = Convert.ToString(row["Piezīmes"]);
db16.STatbPers.Add(STatbPers);
}
else
{
STatbPers.Reģions = Convert.ToString(row["Reģions"]);
STatbPers.Objekts = Convert.ToString(row["Objekts"]);
STatbPers.Ēka = Convert.ToString(row["Ēka"]);
STatbPers.Personāls = Convert.ToString(row["Personāls"]);
STatbPers.Tālrunis = Convert.ToString(row["Tālrunis"]);
STatbPers.NĪAF = Convert.ToString(row["NĪAF"]);
STatbPers.Tālrunis1 = Convert.ToString(row["Tālrunis1"]);
STatbPers.Dispečerdienests = Convert.ToString(row["Dispečerdienests"]);
STatbPers.Tālrunis2 = Convert.ToString(row["Tālrunis2"]);
STatbPers.FDDDS = Convert.ToString(row["FDDDS"]);
STatbPers.Tālrunis3 = Convert.ToString(row["Tālrunis3"]);
STatbPers.Piezīmes = Convert.ToString(row["Piezīmes"]);
}
}
foreach (STatbPers STatbPers in db16.STatbPers)
if (table16.Rows.Find(STatbPers.ID) == null)
db16.STatbPers.Remove(STatbPers);
db16.SaveChanges();
}
private void button1_Click(object sender, EventArgs e)
{
System.Data.DataTable table16 = LoadExcelSheetToTable16(@"\\w-fsclu1-str\STR\LE\DF\DVKC_DS\Fēliksam\2020\WindowsFormsApp3\WindowsFormsApp3\bin\Debug\STatbPers.xlsx", "ST Atbildīgie");
PlaceTableToDatabase16(table16);
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM STatbPers", sqlCon);
System.Data.DataTable dtbl = new System.Data.DataTable();
sqlDa.Fill(dtbl);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dtbl;
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.RowIndex < 1 || e.ColumnIndex < 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top = dataGridView1.AdvancedCellBorderStyle.Top;
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.Value = "";
e.FormattingApplied = true;
}
}
private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.RowHeadersWidthSizeMode =
DataGridViewRowHeadersWidthSizeMode.DisableResizing;
dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[6].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[7].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[8].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[9].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[10].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[11].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[12].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[13].SortMode = DataGridViewColumnSortMode.NotSortable;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
int heightOffset5 = -1;
int widthOffset5 = -2;
int xOffset5 = 1295;
int yOffset5 = 24;
int columnIndex5 = 1;
int columnCount5 = 3;
System.Drawing.Rectangle headerCellRectangle5 = dataGridView1.GetCellDisplayRectangle(columnIndex5, -1, true);
int xCord5 = headerCellRectangle5.Location.X + xOffset5;
int yCord5 = headerCellRectangle5.Location.Y - headerCellRectangle5.Height + yOffset5;
int mergedHeaderWidth5 = dataGridView1.Columns[columnIndex5].Width + dataGridView1.Columns[columnIndex5 + columnCount5 - 1].Width + widthOffset5;
System.Drawing.Rectangle mergedHeaderRect5 = new System.Drawing.Rectangle(xCord5, yCord5, mergedHeaderWidth5, headerCellRectangle5.Height + heightOffset5);
e.Graphics.FillRectangle(new SolidBrush(Color.White), mergedHeaderRect5);
e.Graphics.DrawString("FDD DS/Tālrunis", dataGridView1.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, xCord5 + 45, yCord5 + 3);
int heightOffset = -1;
int widthOffset = -2;
int xOffset = 1;
int yOffset = 24;
int columnIndex = 4;
int columnCount = 6;
System.Drawing.Rectangle headerCellRectangle = dataGridView1.GetCellDisplayRectangle(columnIndex, -1, true);
int xCord = headerCellRectangle.Location.X + xOffset;
int yCord = headerCellRectangle.Location.Y - headerCellRectangle.Height + yOffset;
int mergedHeaderWidth = dataGridView1.Columns[columnIndex].Width + dataGridView1.Columns[columnIndex + columnCount - 1].Width + widthOffset;
System.Drawing.Rectangle mergedHeaderRect = new System.Drawing.Rectangle(xCord, yCord, mergedHeaderWidth, headerCellRectangle.Height + heightOffset);
e.Graphics.FillRectangle(new SolidBrush(Color.White), mergedHeaderRect);
e.Graphics.DrawString("Personāls/Tālrunis", dataGridView1.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, xCord + 120, yCord + 3);
int heightOffset1 = -1;
int widthOffset1 = -2;
int xOffset1 = 330;
int yOffset1 = 24;
int columnIndex1 = 6;
int columnCount1 = 8;
System.Drawing.Rectangle headerCellRectangle1 = dataGridView1.GetCellDisplayRectangle(columnIndex1, -1, true);
int xCord1 = headerCellRectangle.Location.X + xOffset1;
int yCord1 = headerCellRectangle.Location.Y - headerCellRectangle.Height + yOffset1;
int mergedHeaderWidth1 = dataGridView1.Columns[columnIndex1].Width + dataGridView1.Columns[columnIndex1 + columnCount1 - 1].Width + widthOffset1;
System.Drawing.Rectangle mergedHeaderRect1 = new System.Drawing.Rectangle(xCord1, yCord1, mergedHeaderWidth, headerCellRectangle.Height + heightOffset1);
e.Graphics.FillRectangle(new SolidBrush(Color.White), mergedHeaderRect1);
e.Graphics.DrawString("NĪAF/Tālrunis", dataGridView1.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, xCord1 + 130, yCord1 + 3);
int heightOffset3 = -1;
int widthOffset3 = -2;
int xOffset3 = 660;
int yOffset3 = 24;
int columnIndex3 = 4;
int columnCount3 = 6;
System.Drawing.Rectangle headerCellRectangle3 = dataGridView1.GetCellDisplayRectangle(columnIndex3, -1, true);
int xCord3 = headerCellRectangle.Location.X + xOffset3;
int yCord3 = headerCellRectangle.Location.Y - headerCellRectangle.Height + yOffset3;
int mergedHeaderWidth3 = dataGridView1.Columns[columnIndex3].Width + dataGridView1.Columns[columnIndex3 + columnCount3 -1].Width + widthOffset3;
System.Drawing.Rectangle mergedHeaderRect3 = new System.Drawing.Rectangle(xCord3, yCord3, mergedHeaderWidth, headerCellRectangle.Height + heightOffset3);
e.Graphics.FillRectangle(new SolidBrush(Color.White), mergedHeaderRect3);
e.Graphics.DrawString("Dispečerdienests/Tālrunis", dataGridView1.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, xCord3 + 100, yCord3 + 3);
}
}
} |