[.NET]
ต้องการดึงข้อมูลจาก sql มาแสดงใน datagridview แค่บางคอลัมภ์ ค่ะ ส่วนคอลัมภ์ที่เหลือ จะให้ user ทำการกรอกเอง ตอนนี้สร้าง คอลัมภ์มาแล้วค่ะ แต่ติดเรื่องการดึงข้อมูลจาก sql มาแสดงใน คอลัมภ์ค่ะ รบกวนช่วยสอน หน่อยค่ะ
ต้องการดึงข้อมูลจาก sql มาแสดงใน datagridview แค่บางคอลัมภ์ ค่ะ ส่วนคอลัมภ์ที่เหลือ จะให้ user ทำการกรอกเอง ตอนนี้สร้าง คอลัมภ์มาแล้วค่ะ แต่ติดเรื่องการดึงข้อมูลจาก sql มาแสดงใน คอลัมภ์ค่ะ รบกวนช่วยสอน หน่อยค่ะ
Code
Sub setHeaderGridview()
'DataGridView1.Columns(0).HeaderText = "วันที่"
'DataGridView1.Columns(1).HeaderText = "เป้าหมาย"
'DataGridView1.Columns(2).HeaderText = "ยอดโอน"
'DataGridView1.Columns(3).HeaderText = "ยอดโอน(PCS)"
'DataGridView1.Columns(4).HeaderText = "Diff"
Me.DataGridView1.Columns.Add("เป้าหมายจริง", "เป้าหมายจริง") '5 '0
Me.DataGridView1.Columns.Add("กะ1", "กะ1") '6 '1
Me.DataGridView1.Columns.Add("กะ2", "กะ2") '7 '2
Me.DataGridView1.Columns.Add("รวม", "รวม") '8 '3
Me.DataGridView1.Columns.Add("กะ1", "กะ1") '9 '4
Me.DataGridView1.Columns.Add("กะ2", "กะ2") '10 '5
Me.DataGridView1.Columns.Add("รวม", "รวม") '11 '6
Tag : .NET, Ms SQL Server 2008, VB.NET, Windows
Date :
2019-05-15 10:14:51
By :
nunan13
View :
2551
Reply :
12
ลองทำแบบโค๊ดที่แปะ ได้ผลลัพธ์ตามต้องการอยู่
แต่มันมาติดปัญหา ตอนนับคอลัมภ์ มันแยกกัน เวลาที่นำค่าในแต่ละคอลัมมาคำนวณ เลยติดปัญหา อย่างเช่น จะเอา ยอดคอลัมภ์ที่ 2 คือยอดโอน มาคำนวณ แต่ในโปรแกรมมอง ช่อง กะ 2 เป็น คอลัมภ์ที่ 2 อย่างนี้จะต้องแก้ยังไงจะต้องทำยังไงคะ
Code
001.
Sub
Viewdatagridview()
002.
003.
004.
Dim
dtAdapter
As
SqlDataAdapter
005.
Dim
monthName
As
String
006.
Dim
dt
As
New
DataTable
007.
008.
monthName = cboMonth02.Text
009.
Dim
monthIndex
As
Integer
= getMonthNumber(MonthName)
010.
011.
012.
strSQL =
" select s.pt_date ,s.pt_target, s.pt_produc, (select sum(t.pk_qty) as pk_qty from PTtranpack t left join PTpack p on t.p_ref = p.p_ref where t.pk_date = s.pt_date and t.p_ref = p.p_ref and p.p_line = '"
& cmbdept.Text.Trim &
"' ) , s.pt_produc - s.pt_target from PTsum s where month(s.pt_date) = '"
& monthIndex &
"' and year(s.pt_date) ='"
& cboYear02.Text &
"' and s.pt_dept = '"
& cmbdept.Text.Trim &
"'"
013.
connection()
014.
dtAdapter =
New
SqlDataAdapter(strSQL, conn)
015.
dtAdapter.Fill(dt)
016.
017.
If
dt.Rows.Count > 0
Then
018.
019.
020.
DataGridView1.DataSource = dt
021.
setHeaderGridview()
022.
023.
End
If
024.
025.
conn.Close()
026.
027.
028.
029.
030.
End
Sub
031.
032.
Sub
setHeaderGridview()
033.
034.
DataGridView1.Columns(0).HeaderText =
"วันที่"
035.
DataGridView1.Columns(1).HeaderText =
"เป้าหมาย"
036.
DataGridView1.Columns(2).HeaderText =
"ยอดโอน"
037.
DataGridView1.Columns(3).HeaderText =
"ยอดโอน(PCS)"
038.
DataGridView1.Columns(4).HeaderText =
"Diff"
039.
040.
Me
.DataGridView1.Columns.Add(
"เป้าหมายจริง"
,
"เป้าหมายจริง"
)
041.
Me
.DataGridView1.Columns.Add(
"กะ1"
,
"กะ1"
)
042.
Me
.DataGridView1.Columns.Add(
"กะ2"
,
"กะ2"
)
043.
Me
.DataGridView1.Columns.Add(
"รวม"
,
"รวม"
)
044.
Me
.DataGridView1.Columns.Add(
"กะ1"
,
"กะ1"
)
045.
Me
.DataGridView1.Columns.Add(
"กะ2"
,
"กะ2"
)
046.
Me
.DataGridView1.Columns.Add(
"รวม"
,
"รวม"
)
047.
Me
.DataGridView1.Columns.Add(
"หยุดงาน"
,
"หยุดงาน"
)
048.
Me
.DataGridView1.Columns.Add(
"ปกติ 8 ชม"
,
"ปกติ 8 ชม"
)
049.
Me
.DataGridView1.Columns.Add(
"ชม. OT"
,
"ชม. OT"
)
050.
Me
.DataGridView1.Columns.Add(
"OT Manual"
,
"OT Manual"
)
051.
Me
.DataGridView1.Columns.Add(
"ค่าแรงปกติ"
,
"ค่าแรงปกติ"
)
052.
Me
.DataGridView1.Columns.Add(
"OT/ชม"
,
"OT/ชม"
)
053.
Me
.DataGridView1.Columns.Add(
"ค่าแรงรวม"
,
"ค่าแรงรวม"
)
054.
Me
.DataGridView1.Columns.Add(
"% OT"
,
"% OT"
)
055.
Me
.DataGridView1.Columns.Add(
"% ยอดแพ็ค"
,
"% ยอดแพ็ค"
)
056.
Me
.DataGridView1.Columns.Add(
"ปกติ"
,
"ปกติ"
)
057.
Me
.DataGridView1.Columns.Add(
"OT"
,
"OT"
)
058.
Me
.DataGridView1.Columns.Add(
"รวม"
,
"รวม"
)
059.
Me
.DataGridView1.Columns.Add(
"% OT"
,
"% OT"
)
060.
Me
.DataGridView1.Columns.Add(
"% ค่าแรง/PACK"
,
"% ค่าแรง/PACK"
)
061.
Me
.DataGridView1.Columns.Add(
"ปกติ"
,
"ปกติ"
)
062.
Me
.DataGridView1.Columns.Add(
"OT"
,
"OT"
)
063.
Me
.DataGridView1.Columns.Add(
"ปกติ"
,
"ปกติ"
)
064.
Me
.DataGridView1.Columns.Add(
"OT"
,
"OT"
)
065.
Me
.DataGridView1.Columns.Add(
"รวมค่าแรงทั้งหมด"
,
"รวมค่าแรงทั้งหมด"
)
066.
Me
.DataGridView1.Columns.Add(
"% OT"
,
"% OT"
)
067.
Me
.DataGridView1.Columns.Add(
"% ต่อยอด PACK [Target 12%]"
,
"% ต่อยอด PACK [Target 12%]"
)
068.
069.
070.
071.
DataGridView1.Columns(0).Width = 110
072.
DataGridView1.Columns(1).Width = 100
073.
DataGridView1.Columns(2).Width = 100
074.
DataGridView1.Columns(3).Width = 100
075.
DataGridView1.Columns(4).Width = 100
076.
DataGridView1.Columns(5).Width = 100
077.
DataGridView1.Columns(6).Width = 50
078.
DataGridView1.Columns(7).Width = 50
079.
DataGridView1.Columns(8).Width = 60
080.
DataGridView1.Columns(9).Width = 50
081.
DataGridView1.Columns(10).Width = 50
082.
DataGridView1.Columns(11).Width = 60
083.
DataGridView1.Columns(12).Width = 100
084.
DataGridView1.Columns(13).Width = 100
085.
DataGridView1.Columns(14).Width = 50
086.
DataGridView1.Columns(15).Width = 100
087.
DataGridView1.Columns(16).Width = 100
088.
DataGridView1.Columns(17).Width = 100
089.
DataGridView1.Columns(18).Width = 120
090.
DataGridView1.Columns(19).Width = 100
091.
DataGridView1.Columns(20).Width = 80
092.
DataGridView1.Columns(21).Width = 100
093.
DataGridView1.Columns(22).Width = 100
094.
DataGridView1.Columns(23).Width = 120
095.
DataGridView1.Columns(24).Width = 80
096.
DataGridView1.Columns(25).Width = 80
097.
DataGridView1.Columns(26).Width = 120
098.
DataGridView1.Columns(27).Width = 120
099.
DataGridView1.Columns(28).Width = 100
100.
DataGridView1.Columns(29).Width = 100
101.
DataGridView1.Columns(30).Width = 120
102.
DataGridView1.Columns(31).Width = 80
103.
DataGridView1.Columns(32).Width = 100
104.
DataGridView1.Columns(1).DefaultCellStyle.Format =
"#,###.00"
105.
DataGridView1.Columns(2).DefaultCellStyle.Format =
"#,###.00"
106.
DataGridView1.Columns(3).DefaultCellStyle.Format =
"#,###.00"
107.
DataGridView1.Columns(4).DefaultCellStyle.Format =
"#,###.00"
108.
DataGridView1.Columns(5).DefaultCellStyle.Format =
"#,###.00"
109.
110.
DataGridView1.Columns(0).
ReadOnly
=
True
111.
DataGridView1.Columns(1).
ReadOnly
=
True
112.
DataGridView1.Columns(2).
ReadOnly
=
True
113.
DataGridView1.Columns(3).
ReadOnly
=
True
114.
DataGridView1.Columns(4).
ReadOnly
=
True
115.
DataGridView1.Columns(8).
ReadOnly
=
True
116.
DataGridView1.Columns(11).
ReadOnly
=
True
117.
118.
119.
DataGridView1.Columns(12).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
120.
DataGridView1.Columns(25).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
121.
DataGridView1.Columns(32).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
122.
123.
124.
DataGridView1.Font =
New
Font(
"Microsoft Sans Serif"
, 11, FontStyle.Regular)
125.
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
126.
DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
127.
DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
128.
DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
129.
DataGridView1.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
130.
DataGridView1.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
131.
DataGridView1.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
132.
DataGridView1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
133.
DataGridView1.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
134.
DataGridView1.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
135.
DataGridView1.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
136.
DataGridView1.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
137.
DataGridView1.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
138.
DataGridView1.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
139.
DataGridView1.Columns(13).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
140.
DataGridView1.Columns(14).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
141.
DataGridView1.Columns(15).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
142.
DataGridView1.Columns(16).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
143.
DataGridView1.Columns(17).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
144.
DataGridView1.Columns(18).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
145.
DataGridView1.Columns(19).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
146.
DataGridView1.Columns(20).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
147.
DataGridView1.Columns(21).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
148.
DataGridView1.Columns(22).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
149.
DataGridView1.Columns(23).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
150.
DataGridView1.Columns(24).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
151.
DataGridView1.Columns(25).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
152.
DataGridView1.Columns(26).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
153.
DataGridView1.Columns(27).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
154.
DataGridView1.Columns(28).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
155.
DataGridView1.Columns(29).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
156.
DataGridView1.Columns(30).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
157.
DataGridView1.Columns(31).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
158.
DataGridView1.Columns(32).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
159.
160.
161.
DataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
162.
DataGridView1.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
163.
DataGridView1.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
164.
DataGridView1.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
165.
DataGridView1.Columns(4).SortMode = DataGridViewColumnSortMode.NotSortable
166.
DataGridView1.Columns(5).SortMode = DataGridViewColumnSortMode.NotSortable
167.
DataGridView1.Columns(6).SortMode = DataGridViewColumnSortMode.NotSortable
168.
DataGridView1.Columns(7).SortMode = DataGridViewColumnSortMode.NotSortable
169.
DataGridView1.Columns(8).SortMode = DataGridViewColumnSortMode.NotSortable
170.
DataGridView1.Columns(9).SortMode = DataGridViewColumnSortMode.NotSortable
171.
DataGridView1.Columns(10).SortMode = DataGridViewColumnSortMode.NotSortable
172.
DataGridView1.Columns(11).SortMode = DataGridViewColumnSortMode.NotSortable
173.
DataGridView1.Columns(12).SortMode = DataGridViewColumnSortMode.NotSortable
174.
DataGridView1.Columns(13).SortMode = DataGridViewColumnSortMode.NotSortable
175.
DataGridView1.Columns(14).SortMode = DataGridViewColumnSortMode.NotSortable
176.
DataGridView1.Columns(14).SortMode = DataGridViewColumnSortMode.NotSortable
177.
DataGridView1.Columns(14).SortMode = DataGridViewColumnSortMode.NotSortable
178.
DataGridView1.Columns(15).SortMode = DataGridViewColumnSortMode.NotSortable
179.
DataGridView1.Columns(16).SortMode = DataGridViewColumnSortMode.NotSortable
180.
DataGridView1.Columns(17).SortMode = DataGridViewColumnSortMode.NotSortable
181.
DataGridView1.Columns(18).SortMode = DataGridViewColumnSortMode.NotSortable
182.
DataGridView1.Columns(19).SortMode = DataGridViewColumnSortMode.NotSortable
183.
DataGridView1.Columns(20).SortMode = DataGridViewColumnSortMode.NotSortable
184.
DataGridView1.Columns(21).SortMode = DataGridViewColumnSortMode.NotSortable
185.
DataGridView1.Columns(22).SortMode = DataGridViewColumnSortMode.NotSortable
186.
DataGridView1.Columns(23).SortMode = DataGridViewColumnSortMode.NotSortable
187.
DataGridView1.Columns(24).SortMode = DataGridViewColumnSortMode.NotSortable
188.
DataGridView1.Columns(25).SortMode = DataGridViewColumnSortMode.NotSortable
189.
DataGridView1.Columns(26).SortMode = DataGridViewColumnSortMode.NotSortable
190.
DataGridView1.Columns(27).SortMode = DataGridViewColumnSortMode.NotSortable
191.
DataGridView1.Columns(28).SortMode = DataGridViewColumnSortMode.NotSortable
192.
DataGridView1.Columns(29).SortMode = DataGridViewColumnSortMode.NotSortable
193.
DataGridView1.Columns(30).SortMode = DataGridViewColumnSortMode.NotSortable
194.
DataGridView1.Columns(31).SortMode = DataGridViewColumnSortMode.NotSortable
195.
DataGridView1.Columns(32).SortMode = DataGridViewColumnSortMode.NotSortable
196.
DataGridView1.Columns(0).HeaderCell.Style.BackColor = Color.LightGreen
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
Me
.DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
208.
Me
.DataGridView1.ColumnHeadersHeight =
Me
.DataGridView1.ColumnHeadersHeight * 3.3
209.
Me
.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
210.
211.
End
Sub
Date :
2019-05-15 11:03:57
By :
nunan13
งง code มาก
1.ต้องการดึงข้อมูลจาก sql มาแสดงใน datagridview แค่บางคอลัมภ์
ตอบ ก็ใช้คำสั่ง SQL ดึงข้อมูลมาตาม column ที่ ต้องการ ครับ หรือ ถ้าดึงมาแบบ *(Star)
ก็มาลบใน DATATABLE เอา ครับ table.column("TEST").removeAT("TEST")
ประวัติการแก้ไข 2019-05-15 14:10:27
Date :
2019-05-15 14:09:08
By :
2127832830566218
จะดึงข้อมูลบางส่วนมาจาก DB ลงใน DataGridView แล้วให้ user กรอกข้อมูลเพิ่มบางส่วน ใน DataGridView นั้น ใช่ไหมครับ
Date :
2019-05-15 15:08:33
By :
CPU4Core
ตอบความคิดเห็นที่ : 3 เขียนโดย : CPU4Core เมื่อวันที่ 2019-05-15 15:08:33
รายละเอียดของการตอบ ::
ใช่ค่ะ พอดีทำตารางแบบมีการ mage แล้วไม่ค่อยคุ้นเท่าไหร่ ตอนที่ทำตารางสร้างคอลัมภ์ สร้างแบบ นี้ค่ะ
Me.DataGridView1.Columns.Add("เป้าหมายจริง", "เป้าหมายจริง") ******** คอลัมภ์นี้ต้องการดึงข้อมูลจาก sql มาแสดงค่ะ
Me.DataGridView1.Columns.Add("กะ1", "กะ1") '6 '1 ส่วน คอลัภม์นี้ ต้องการให้ user กรอกเอง
ตอนที่ทำการ select ข้อมูล ทำแบบนี้ค่ะ
Code (VB.NET)
01.
Dim
dtAdapter
As
SqlDataAdapter
02.
Dim
monthName
As
String
03.
Dim
dt
As
New
DataTable
04.
05.
monthName = cboMonth02.Text
06.
Dim
monthIndex
As
Integer
= getMonthNumber(MonthName)
07.
08.
09.
strSQL =
" select s.pt_date ,s.pt_target, s.pt_produc, (select sum(t.pk_qty) as pk_qty from PTtranpack t left join PTpack p on t.p_ref = p.p_ref where t.pk_date = s.pt_date and t.p_ref = p.p_ref and p.p_line = '"
& cmbdept.Text.Trim &
"' ) , s.pt_produc - s.pt_target from PTsum s where month(s.pt_date) = '"
& monthIndex &
"' and year(s.pt_date) ='"
& cboYear02.Text &
"' and s.pt_dept = '"
& cmbdept.Text.Trim &
"'"
10.
connection()
11.
dtAdapter =
New
SqlDataAdapter(strSQL, conn)
12.
dtAdapter.Fill(dt)
13.
14.
If
dt.Rows.Count > 0
Then
15.
16.
17.
DataGridView1.DataSource = dt
18.
setHeaderGridview()
19.
20.
End
If
21.
22.
conn.Close()
********************* แต่ไม่รู้จะนำที่ select มา ยัดใส่ใน datagrid ที่ชื่อ Me.DataGridView1.Columns.Add("เป้าหมายจริง", "เป้าหมายจริง") ยังไงค่ะ
Date :
2019-05-15 15:24:11
By :
nunan13
ลองดูนะครับ ไม่แน่ใจว่าผมเข้าใจเจ้าของกระทู้หรือป่าว จากนั้นก็วนลูป Save เมื่อ User ทำการ Input data...
Code (SQL)
01.
USE [THAI_CREATE]
02.
GO
03.
/****** Object:
Table
[dbo].[Cars] Script
Date
: 15/05/2019 17:12:14 ******/
04.
SET
ANSI_NULLS
ON
05.
GO
06.
SET
QUOTED_IDENTIFIER
ON
07.
GO
08.
CREATE
TABLE
[dbo].[Cars](
09.
[ID] [
int
] IDENTITY(1,1)
NOT
NULL
,
10.
[Trademark] [nvarchar](50)
NULL
,
11.
[Model] [nvarchar](50)
NULL
,
12.
[HP] [
smallint
]
NULL
,
13.
[Liter] [
float
]
NULL
,
14.
[Cyl] [
smallint
]
NULL
,
15.
[TransmissSpeedCount] [
smallint
]
NULL
,
16.
[TransmissAutomatic] [nvarchar](3)
NULL
,
17.
[MPG_City] [
smallint
]
NULL
,
18.
[MPG_Highway] [
smallint
]
NULL
,
19.
[Category] [nvarchar](7)
NULL
,
20.
[Description] [ntext]
NULL
,
21.
[Hyperlink] [nvarchar](50)
NULL
,
22.
[Picture] [image]
NULL
,
23.
[Price] [money]
NULL
,
24.
[RtfContent] [ntext]
NULL
,
25.
CONSTRAINT
[PK_Cars]
PRIMARY
KEY
CLUSTERED
26.
(
27.
[ID]
ASC
28.
)
WITH
(PAD_INDEX =
OFF
, STATISTICS_NORECOMPUTE =
OFF
, IGNORE_DUP_KEY =
OFF
, ALLOW_ROW_LOCKS =
ON
, ALLOW_PAGE_LOCKS =
ON
)
ON
[
PRIMARY
]
29.
)
ON
[
PRIMARY
] TEXTIMAGE_ON [
PRIMARY
]
30.
31.
GO
32.
SET
IDENTITY_INSERT [dbo].[Cars]
ON
33.
34.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(1, N
'Mercedes-Benz'
, N
'SL500 Roadster'
, 302, 4.966, 8, 5, N
'Yes'
, 16, 23, N
'SPORTS'
,
NULL
, N
'http://www.mercedes.com '
,
NULL
, 83800.0000,
NULL
)
35.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(2, N
'Mercedes-Benz'
, N
'CLK55 AMG Cabriolet'
, 342, 5.439, 8, 5, N
'Yes'
, 17, 24, N
'SPORTS'
,
NULL
, N
'http://www.mercedes.com '
,
NULL
, 79645.0000,
NULL
)
36.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(3, N
'Mercedes-Benz'
, N
'C230 Kompressor Sport Coupe'
, 189, 1.796, 4, 5, N
'Yes'
, 21, 28, N
'SPORTS'
,
NULL
, N
'http://www.mercedes.com '
,
NULL
, 25600.0000,
NULL
)
37.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(4, N
'BMW'
, N
'530i'
, 225, 3, 6, 5, N
'No'
, 21, 30, N
'SALOON'
,
NULL
, N
'http://www.bmw.com '
,
NULL
, 39450.0000,
NULL
)
38.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(5, N
'Rolls-Royce'
, N
'Corniche'
, 325, 6.75, 8, 4, N
'Yes'
, 11, 16, N
'SALOON'
,
NULL
, N
'http://www.rollsroyce.com '
,
NULL
, 370485.0000,
NULL
)
39.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(6, N
'Jaguar'
, N
'S-Type 3.0'
, 235, 3, 6, 5, N
'No'
, 18, 25, N
'SALOON'
,
NULL
, N
'http://www.jaguar.com '
,
NULL
, 44320.0000,
NULL
)
40.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(7, N
'Cadillac'
, N
'Seville'
, 275, 4.6, 8, 4, N
'Yes'
, 18, 27, N
'SALOON'
,
NULL
, N
'http://www.cadillac.com '
,
NULL
, 49600.0000,
NULL
)
41.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(8, N
'Cadillac'
, N
'DeVille'
, 275, 4.6, 8, 4, N
'Yes'
, 18, 27, N
'SALOON'
,
NULL
, N
'http://www.cadillac.com '
,
NULL
, 47780.0000,
NULL
)
42.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(9, N
'Lexus'
, N
'LS430'
, 290, 4.3, 8, 5, N
'Yes'
, 18, 25, N
'SALOON'
,
NULL
, N
'http://www.lexus.com '
,
NULL
, 54900.0000,
NULL
)
43.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(10, N
'Lexus'
, N
'GS 430'
, 300, 4.3, 8, 5, N
'Yes'
, 18, 23, N
'SALOON'
,
NULL
, N
'http://www.lexus.com '
,
NULL
, 41242.0000,
NULL
)
44.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(11, N
'Ford'
, N
'Ranger FX-4'
, 135, 2.3, 4, 5, N
'Yes'
, 21, 25, N
'TRUCK'
,
NULL
, N
'http://www.ford.com '
,
NULL
, 12565.0000,
NULL
)
45.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(12, N
'Dodge'
, N
'Ram 1500'
, 215, 3.7, 6, 4, N
'Yes'
, 15, 19, N
'TRUCK'
,
NULL
, N
'http://www.dodge.com '
,
NULL
, 17315.0000,
NULL
)
46.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(13, N
'GMC'
, N
'Siera Quadrasteer'
, 200, 4.3, 6, 4, N
'Yes'
, 15, 20, N
'TRUCK'
,
NULL
, N
'http://www.gmc.com '
,
NULL
, 17748.0000,
NULL
)
47.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(14, N
'Nissan'
, N
'Crew Cab SE'
, 143, 2.4, 4, 4, N
'Yes'
, 20, 23, N
'TRUCK'
,
NULL
, N
'http://www.NissanDriven.com '
,
NULL
, 12800.0000,
NULL
)
48.
INSERT
[dbo].[Cars] ([ID], [Trademark], [Model], [HP], [Liter], [Cyl], [TransmissSpeedCount], [TransmissAutomatic], [MPG_City], [MPG_Highway], [Category], [Description], [Hyperlink], [Picture], [Price], [RtfContent])
VALUES
(15, N
'Toyota'
, N
'Tacoma S-Runner'
, 190, 3.4, 6, 5, N
'No'
, 18, 22, N
'TRUCK'
,
NULL
, N
'http://www.toyota.com '
,
NULL
, 20000.0000,
NULL
)
49.
SET
IDENTITY_INSERT [dbo].[Cars]
OFF
50.
ALTER
TABLE
[dbo].[Cars]
ADD
DEFAULT
((0))
FOR
[HP]
51.
GO
52.
ALTER
TABLE
[dbo].[Cars]
ADD
DEFAULT
((0))
FOR
[Liter]
53.
GO
54.
ALTER
TABLE
[dbo].[Cars]
ADD
DEFAULT
((0))
FOR
[Cyl]
55.
GO
56.
ALTER
TABLE
[dbo].[Cars]
ADD
DEFAULT
((0))
FOR
[TransmissSpeedCount]
57.
GO
58.
ALTER
TABLE
[dbo].[Cars]
ADD
DEFAULT
((0))
FOR
[MPG_City]
59.
GO
60.
ALTER
TABLE
[dbo].[Cars]
ADD
DEFAULT
((0))
FOR
[MPG_Highway]
61.
GO
62.
ALTER
TABLE
[dbo].[Cars]
ADD
DEFAULT
((0))
FOR
[Price]
63.
GO
Code (VB.NET)
01.
Imports
System.IO
02.
Imports
System.Data
03.
Imports
System.Data.SqlClient
04.
05.
06.
Public
Class
Form1
07.
Dim
strCon
As
String
=
"Server=127.0.0.1;Database=THAI_CREATE;User ID=sa;Password=P@ssw0rd69;"
08.
Dim
DbConn
As
New
SqlConnection(strCon)
09.
10.
Private
Sub
Button1_Click(sender
As
Object
, e
As
EventArgs)
Handles
Button1.Click
11.
Dim
dtClm
As
New
DataTable()
12.
With
dtClm
13.
.Columns.Add(
"Brand"
,
GetType
(
String
))
14.
.Columns.Add(
"Model"
,
GetType
(
String
))
15.
.Columns.Add(
"Price"
,
GetType
(
String
))
16.
17.
18.
.Columns.Add(
"เป้าหมายจริง"
,
GetType
(
String
))
19.
.Columns.Add(
"กะ1"
,
GetType
(
String
))
20.
.Columns.Add(
"กะ2"
,
GetType
(
String
))
21.
End
With
22.
23.
DbConn.Open()
24.
Dim
row
As
DataRow
25.
Dim
sql
As
String
= <sql>select * from Cars</sql>
26.
Dim
da
As
New
SqlDataAdapter(sql, DbConn)
27.
Dim
dt
As
New
DataTable(
"TB"
)
28.
da.Fill(dt)
29.
30.
For
i = 0
To
dt.Rows.Count - 1
31.
Dim
Brand
As
String
= dt.Rows(i).Item(
"Trademark"
)
32.
Dim
Model
As
String
= dt.Rows(i).Item(
"Model"
)
33.
Dim
Price
As
String
= FormatNumber(dt.Rows(i).Item(
"Price"
), 2)
34.
row = dtClm.Rows.Add(Brand, Model, Price)
35.
Next
36.
DataGridView1.DataSource = dtClm
37.
38.
DbConn.Close()
39.
End
Sub
40.
End
Class
Output--->ก็จะประมาณนี้ครับ
ประวัติการแก้ไข 2019-05-15 17:17:25
Date :
2019-05-15 17:14:09
By :
beerkingdom
ลอง
select field1, field2, field3, '' as input1, '' as input2, '' as input3
from tablename
สีแดงเป็น field ว่างเตรียมใส่ข้อมูล
Date :
2019-05-15 18:23:59
By :
Chaidhanan
For i = 0 To DataGridView1.Rows.Count - 1
แก้เป็น For i = 0 To DataGridView1.Rows.Count - 2
เพราะ Rows.count -1 คือบันทัด กรอบแดงหรือเปล่า (เดาเอาจากรูปภาพ)
Date :
2019-05-20 20:03:48
By :
Chaidhanan
Load balance : Server 01