Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > สอบถามครับ อยากทราบวิธี SELECT ข้อมูลเฉพาะ Column ที่ต้องการแล้วมาใส่ใน DGV ตามตําแหน่ง Column ที่สร้างเอาไว้แล้วสามารถทำได้ไหมครับ ที่ไม่ใช้ Dataset ครับ



 

สอบถามครับ อยากทราบวิธี SELECT ข้อมูลเฉพาะ Column ที่ต้องการแล้วมาใส่ใน DGV ตามตําแหน่ง Column ที่สร้างเอาไว้แล้วสามารถทำได้ไหมครับ ที่ไม่ใช้ Dataset ครับ

 



Topic : 126886



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์




อยากทราบวิธีการที่จะนำ ข้อมูลใน sql มาลงใน DGV ที่ มี Column ไว้แล้ว ที่ไม่ใช่วิธี Dataset ครับ พอทำได้ไหมครับ ขอบคุณครับ



Tag : .NET, Ms SQL Server 2012, Win (Windows App), VB.NET









ประวัติการแก้ไข
2017-03-20 19:50:24
2017-03-20 21:20:03
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-03-20 19:48:39 By : BorbbanG View : 733 Reply : 13
 

 

No. 1



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

http://ที่ไม่ใช่วิธี Dataset ครับ

แล้วผ่าน DataTable หรือ DataAdapter ได้ไม๊ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-20 20:55:59 By : lamaka.tor
 


 

No. 2



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์


ครับ เป็น Win app ครับ ใช้ DataTable กับ DataAdapter ครับ
ไม่ทราบว่าเขียนยังไงได้บ้างครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-20 21:08:46 By : BorbbanG
 

 

No. 3



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 2 เขียนโดย : BorbbanG เมื่อวันที่ 2017-03-20 21:08:46
รายละเอียดของการตอบ ::

ประมาณนี้พอไหวไม๊ครับ
Code (VB.NET)
Dim strSql As String = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee"
Dim dtb As New DataTable
Using cnn As New SqlConnection(connectionString)
  cnn.Open()
  Using dad As New SqlDataAdapter(strSql, cnn)
    dad.Fill(dtb)
  End Using
  cnn.Close()
End Using


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-20 22:16:34 By : lamaka.tor
 


 

No. 4



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์


คือผมลองทำแล้วครับ Add ลงได้แถวเดียว คือ ผมมีข้อมูลที่ต้องการหลายแถว จะให้ Add ลง ทั้งหมดทุกแถวได้ไหม

นั้นนี้โค้ตครับ

Code (VB.NET)
  Private Sub SetDGV1()
        Dim sqlDcode As String

        Dim dtDcode As New DataTable
        Dim sb As New StringBuilder
        Dim Comm As New SqlCommand
        If TextBox4.Text.Length <> 0 Then
            sb.Remove(0, sb.Length)
            sb.Append("SELECT * FROM tbl_solution_w ")
            '          
            sb.Append("where id_W=@ID  ORDER BY date DESC ")


            With Comm
                .Parameters.Clear()
                .Parameters.AddWithValue("@ID", SqlDbType.NVarChar).Value = TextBox4.Text
            End With

            sqlDcode = sb.ToString()
            dtDcode = ZSQLDataReader("Drugstore", sqlDcode, Comm)
            DataGridView2.DataSource = dtDcode
           
            If dtDcode.Rows.Count <> 0 Then
                DataGridView1.Rows.Add()

                Dim i As Integer
                i = DataGridView1.Rows.Count - 1

                Label25.Text = dtDcode.Rows(0)(1).ToString
                Label26.Text = dtDcode.Rows(0)(2).ToString
                TextBox4.Text = dtDcode.Rows(0)(3).ToString
                cmb_dept.Text = dtDcode.Rows(0)(7).ToString
                DateTimePicker1.Value = dtDcode.Rows(0)(8).ToString
                Label20.Text = dtDcode.Rows(0)(15).ToString
                Label22.Text = dtDcode.Rows(0)(16).ToString
                cmb_psell.Text = dtDcode.Rows(0)(17).ToString
     
                DataGridView1.Item(0, i).Value = dtDcode.Rows(0)(4).ToString
                DataGridView1.Item(1, i).Value = dtDcode.Rows(0)(5).ToString
                DataGridView1.Item(2, i).Value = dtDcode.Rows(0)(6).ToString
                DataGridView1.Item(3, i).Value = dtDcode.Rows(0)(9).ToString
                DataGridView1.Item(4, i).Value = dtDcode.Rows(0)(10).ToString
                DataGridView1.Item(5, i).Value = dtDcode.Rows(0)(11).ToString
                DataGridView1.Item(6, i).Value = dtDcode.Rows(0)(12).ToString
                DataGridView1.Item(7, i).Value = dtDcode.Rows(0)(13).ToString
                DataGridView1.Item(8, i).Value = dtDcode.Rows(0)(14).ToString
              
            End If

        End If
      
        searchdeptID()
     
    
    End Sub

จากรูป ในกรอบสีแดง คือข้อมูลที่ Add มา
ส่วนสีเหลือง คือ ข้อมูลจริงใน Server sql ครับ

ประกอบคำถาม

รบกวนช่วยหน่อยครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-21 10:11:50 By : BorbbanG
 


 

No. 5



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ได้ครับ ถ้าใช้ for

หรือให้ง่ายกว่านั้น ใช้ data wizard

กด ปุ่มเดียว ได้ทั้ง Insert/Update
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-21 11:25:17 By : lamaka.tor
 


 

No. 6



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์


เพิ่ม for ไป

กก
Code (VB.NET)
  Dim s As Integer
            For s = 0 To dtDcode.Rows.Count - 1
                If dtDcode.Rows.Count <> 0 Then
                    DataGridView1.Rows.Add()

                    Dim i As Integer
                    i = DataGridView1.Rows.Count - 1

                    Label25.Text = dtDcode.Rows(0)(1).ToString
                    Label26.Text = dtDcode.Rows(0)(2).ToString
                    TextBox4.Text = dtDcode.Rows(0)(3).ToString
                    cmb_dept.Text = dtDcode.Rows(0)(7).ToString
                    DateTimePicker1.Value = dtDcode.Rows(0)(8).ToString
                    '   Label20.Text = dtDcode.Rows(0)(15).ToString
                    '   Label22.Text = dtDcode.Rows(0)(16).ToString
                    cmb_psell.Text = dtDcode.Rows(0)(17).ToString

                    DataGridView1.Item(0, i).Value = dtDcode.Rows(0)(4).ToString
                    DataGridView1.Item(1, i).Value = dtDcode.Rows(0)(5).ToString
                    DataGridView1.Item(2, i).Value = dtDcode.Rows(0)(6).ToString
                    DataGridView1.Item(3, i).Value = dtDcode.Rows(0)(9).ToString
                    DataGridView1.Item(4, i).Value = dtDcode.Rows(0)(10).ToString
                    DataGridView1.Item(5, i).Value = dtDcode.Rows(0)(11).ToString
                    DataGridView1.Item(6, i).Value = dtDcode.Rows(0)(12).ToString
                    DataGridView1.Item(7, i).Value = dtDcode.Rows(0)(13).ToString
                    DataGridView1.Item(8, i).Value = dtDcode.Rows(0)(14).ToString

                End If
            Next
            

        End If


แล้วได้ค่าซ้ำครับ ทำไงต่อดี


ประวัติการแก้ไข
2017-03-21 11:51:17
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-21 11:48:11 By : BorbbanG
 


 

No. 7



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

หมายถึง Index ที่มีอยู่แล้วงั้นรึ

ก็บ้านๆ

เช็คก่อนว่ามี Index นั้นๆ หรือไม่

Code
if haveIndex then update data ซะ else Insert data end if

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-21 14:02:56 By : lamaka.tor
 


 

No. 8



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

บ้านๆก็ประมาณนี้ครับ

ลาก ที 2 ที ก็เสร็จ

https://www.thaicreate.com/community/csharp-datasource-wizard.html
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-21 14:08:50 By : lamaka.tor
 


 

No. 9



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์


คือที่ว่า ผมไม่ใช้ datasource wizard

ขอแบบวิธีอื่น พอได้ไหมครับ


ประวัติการแก้ไข
2017-03-21 15:07:36
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-21 15:05:05 By : BorbbanG
 


 

No. 10



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 9 เขียนโดย : BorbbanG เมื่อวันที่ 2017-03-21 15:05:05
รายละเอียดของการตอบ ::
No. 7 เลยครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-21 16:55:53 By : lamaka.tor
 


 

No. 11



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์


รบกวนขอตัวอย่างหน่อยได้ไหมครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-22 11:23:22 By : BorbbanG
 


 

No. 12



โพสกระทู้ ( 119 )
บทความ ( 3 )



สถานะออฟไลน์
Facebook

บ้าน ๆ ครับ แอด row เข้าไปแล้ว แอดข้อมูลลงไปตามคอร์รั่ม ใช้ While ไล่อ่านข้อมูลที่มีทั้งหมดในฐานข้อมูล

Code (VB.NET)
cmd = New MySqlCommand("SELECT user_id,user_name,user_address,user_tel,user_email,user_usernamelogin,user_leval FROM users", ConnectionDB)
            Call open_connection()
            Dr = cmd.ExecuteReader
            While Dr.Read()
                Dim r As Integer
                With Me.DataGridview1
                    r = .RowCount
                    .Rows.Add()
                    .Rows(r).Cells(0).Value = r + 1
                    .Rows(r).Cells(1).Value = Dr.Item("user_id")
                    .Rows(r).Cells(2).Value = Dr.Item("user_name")
                    .Rows(r).Cells(3).Value = Dr.Item("user_address")
                    .Rows(r).Cells(4).Value = Dr.Item("user_tel")
                    .Rows(r).Cells(5).Value = Dr.Item("user_email")
                    .Rows(r).Cells(6).Value = Dr.Item("user_usernamelogin")
                End With
            End While



ประวัติการแก้ไข
2017-03-22 11:59:36
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-22 11:57:44 By : pakyaudio
 


 

No. 13



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณมากครับ ที่ให้การช่วยเหลือ ปรับนิดหน่อยก็ใช้ได้แล้ว ^^

Code (VB.NET)
 Private Sub SetDGV1()
        If DataGridView1.Rows.Count <> 0 Then
            DataGridView1.Rows.Clear()
        End If
        Dim sqlDcode As String
        Dim con As New SqlConnection
        con = ZConnectionOnly("drugstore")
        Dim dtDcode As New DataTable
        Dim sb As New StringBuilder
        Dim Comm As New SqlCommand  
        If TextBox4.Text.Length <> 0 Then
            sb.Remove(0, sb.Length)
            sb.Append("SELECT * FROM tbl_solution_w ")
            sb.Append("where id_W=@ID  ORDER BY date DESC ")
            sqlDcode = sb.ToString()
            With Comm
                .Parameters.Clear()
                .CommandText = sqlDcode
                .Parameters.AddWithValue("@ID", SqlDbType.NVarChar).Value = TextBox4.Text
                .Connection = con

            End With
            dr = Comm.ExecuteReader
            While (dr.Read())
                Dim r As Integer

                With (Me.DataGridView1)
                    r = .RowCount
                    .Rows.Add()
                    .Rows(r).Cells(0).Value = dr.Item("drug_code_homec")
                    .Rows(r).Cells(1).Value = dr.Item("drug_code_invc")
                    .Rows(r).Cells(2).Value = dr.Item("drug_name")
                    .Rows(r).Cells(3).Value = dr.Item("sell")
                    .Rows(r).Cells(4).Value = dr.Item("sumsell")
                    .Rows(r).Cells(5).Value = dr.Item("unit")
                    .Rows(r).Cells(6).Value = dr.Item("price")
                    .Rows(r).Cells(7).Value = dr.Item("sumprice")
                    .Rows(r).Cells(8).Value = dr.Item("status")
                    Label25.Text = dr.Item("id").ToString
                    Label26.Text = dr.Item("hid").ToString
                    cmb_dept.Text = dr.Item("dept_name").ToString
                    DateTimePicker1.Value = dr.Item("date").ToString
                    '   Label20.Text = dtDcode.Rows(0)(15).ToString
                    '   Label22.Text = dtDcode.Rows(0)(16).ToString
                    cmb_psell.Text = dr.Item("psell").ToString
                End With
            End While
        End If
        searchdeptID()
    End Sub

ผ่าน



ประวัติการแก้ไข
2017-03-22 13:21:39
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-03-22 13:21:03 By : BorbbanG
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามครับ อยากทราบวิธี SELECT ข้อมูลเฉพาะ Column ที่ต้องการแล้วมาใส่ใน DGV ตามตําแหน่ง Column ที่สร้างเอาไว้แล้วสามารถทำได้ไหมครับ ที่ไม่ใช้ Dataset ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่