تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
DataGridView
#1
Star 
السلام عليكم و رحمه الله و بركاته اهل المنتدي الكرام ..

أقدم لكم اليوم أكواد لأداة DataGridView :

احفظ رابط الموضوع لكي تحتاجه فيما بعد
http://vb4arb.com/vb/showthread.php?tid=26063


إضافة زر :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)

       Dim btn As New DataGridViewButtonColumn()
       DataGridView1.Columns.Add(btn)
       btn.HeaderText = "Click Data"
       btn.Text = "Click Here"
       btn.Name = "btn"
       btn.UseColumnTextForButtonValue = True

   End Sub
   Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
       If e.ColumnIndex = 3 Then
           MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
       End If
   End Sub
End Class

فلترة :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
       Dim sql As String = "SELECT * FROM Titles"
       Dim connection As New SqlConnection(connectionString)
       Dim dataadapter As New SqlDataAdapter(sql, connection)
       Dim ds As New DataSet()
       connection.Open()
       dataadapter.Fill(ds, "Titles_table")
       connection.Close()

       Dim dv As DataView
       dv = New DataView(ds.Tables(0), "Price > 19", "Price Desc", DataViewRowState.CurrentRows)
       DataGridView1.DataSource = dv
   End Sub
End Class

تعدد الصفحات :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Dim pagingAdapter As SqlDataAdapter
   Dim pagingDS As DataSet
   Dim scrollVal As Integer

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
       Dim sql As String = "SELECT * FROM authors"
       Dim connection As New SqlConnection(connectionString)
       pagingAdapter = New SqlDataAdapter(sql, connection)
       pagingDS = New DataSet()
       connection.Open()
       pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")
       connection.Close()
       DataGridView1.DataSource = pagingDS
       DataGridView1.DataMember = "authors_table"
   End Sub

   Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
       scrollVal = scrollVal - 5
       If scrollVal <= 0 Then
           scrollVal = 0
       End If
       pagingDS.Clear()
       pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")
   End Sub

   Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
       scrollVal = scrollVal + 5
       If scrollVal > 23 Then
           scrollVal = 18
       End If
       pagingDS.Clear()
       pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")
   End Sub
End Class

إضافة صورة :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)

       Dim img As New DataGridViewImageColumn()
       Dim inImg As Image = Image.FromFile("Image Path")
       img.Image = inImg
       DataGridView1.Columns.Add(img)
       img.HeaderText = "Image"
       img.Name = "img"

   End Sub
End Class

إضافة ComboBox :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)

       Dim cmb As New DataGridViewComboBoxColumn()
       cmb.HeaderText = "Select Data"
       cmb.Name = "cmb"
       cmb.MaxDropDownItems = 4
       cmb.Items.Add("True")
       cmb.Items.Add("False")
       DataGridView1.Columns.Add(cmb)

   End Sub
End Class

إضافة Link :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)

       Dim lnk As New DataGridViewLinkColumn()
       DataGridView1.Columns.Add(lnk)
       lnk.HeaderText = "Link Data"
       lnk.Name = "http://vb.net-informations.com"
       lnk.Text = "http://vb.net-informations.com"
       lnk.UseColumnTextForLinkValue = True

   End Sub
End Class

TempLate :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim row As DataGridViewRow = Me.DataGridView1.RowTemplate
       row.DefaultCellStyle.BackColor = Color.Bisque
       row.Height = 35
       row.MinimumHeight = 20

       Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
       Dim sql As String = "SELECT * FROM Authors"
       Dim connection As New SqlConnection(connectionString)
       Dim dataadapter As New SqlDataAdapter(sql, connection)
       Dim ds As New DataSet()
       connection.Open()
       dataadapter.Fill(ds, "Authors_table")
       connection.Close()
       DataGridView1.DataSource = ds
       DataGridView1.DataMember = "Authors_table"

   End Sub
End Class

مربع آختيارات :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)

       Dim chk As New DataGridViewCheckBoxColumn()
       DataGridView1.Columns.Add(chk)
       chk.HeaderText = "Check Data"
       chk.Name = "chk"
       DataGridView1.Rows(2).Cells(3).Value = True

   End Sub
End Class

إضافة صفوف و أعمدة :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)
   End Sub
End Class

إخفاء الصفوف :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)

       DataGridView1.Rows(1).Visible = False

   End Sub
End Class

إخفاء الأعمدة :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       DataGridView1.ColumnCount = 3
       DataGridView1.Columns(0).Name = "Product ID"
       DataGridView1.Columns(1).Name = "Product Name"
       DataGridView1.Columns(2).Name = "Product_Price"

       Dim row As String() = New String() {"1", "Product 1", "1000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"2", "Product 2", "2000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"3", "Product 3", "3000"}
       DataGridView1.Rows.Add(row)
       row = New String() {"4", "Product 4", "4000"}
       DataGridView1.Rows.Add(row)

       DataGridView1.Rows(1).ReadOnly = True

   End Sub
End Class

التنسيق :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
       Dim sql As String = "SELECT * FROM Authors"
       Dim connection As New SqlConnection(connectionString)
       Dim dataadapter As New SqlDataAdapter(sql, connection)
       Dim ds As New DataSet()
       connection.Open()
       dataadapter.Fill(ds, "Authors_table")
       connection.Close()
       DataGridView1.DataSource = ds
       DataGridView1.DataMember = "Authors_table"

       DataGridView1.GridColor = Color.Red
       DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None
       DataGridView1.BackgroundColor = Color.LightGray

       DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red
       DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow

       DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.[True]

       DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
       DataGridView1.AllowUserToResizeColumns = False

       DataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque
       DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
   End Sub
End Class

الطباعة :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
       Dim sql As String = "SELECT * FROM Authors"
       Dim connection As New SqlConnection(connectionString)
       Dim dataadapter As New SqlDataAdapter(sql, connection)
       Dim ds As New DataSet()
       connection.Open()
       dataadapter.Fill(ds, "Authors_table")
       connection.Close()
       DataGridView1.DataSource = ds
       DataGridView1.DataMember = "Authors_table"
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       PrintDocument1.Print()
   End Sub

   Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
       Dim bm As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
       DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
       e.Graphics.DrawImage(bm, 0, 0)
   End Sub
End Class

التصدير إلي الأكسل :

كود :
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
       Dim sql As String = "SELECT * FROM Authors"
       Dim connection As New SqlConnection(connectionString)
       Dim dataadapter As New SqlDataAdapter(sql, connection)
       Dim ds As New DataSet()
       connection.Open()
       dataadapter.Fill(ds, "Authors_table")
       connection.Close()
       DataGridView1.DataSource = ds
       DataGridView1.DataMember = "Authors_table"

   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim xlApp As Excel.Application
       Dim xlWorkBook As Excel.Workbook
       Dim xlWorkSheet As Excel.Worksheet
       Dim misValue As Object = System.Reflection.Missing.Value

       Dim i As Int16, j As Int16

       xlApp = New Excel.ApplicationClass
       xlWorkBook = xlApp.Workbooks.Add(misValue)
       xlWorkSheet = xlWorkBook.Sheets("sheet1")


       For i = 0 To DataGridView1.RowCount - 2
           For j = 0 To DataGridView1.ColumnCount - 1
               xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j, i).Value.ToString()
           Next
       Next

       xlWorkBook.SaveAs("c:\vb.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
        Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
       xlWorkBook.Close(True, misValue, misValue)
       xlApp.Quit()

       releaseObject(xlWorkSheet)
       releaseObject(xlWorkBook)
       releaseObject(xlApp)

       MessageBox.Show("Over")
   End Sub


   Private Sub releaseObject(ByVal obj As Object)
       Try
           System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
           obj = Nothing
       Catch ex As Exception
           obj = Nothing
           MessageBox.Show("Exception Occured while releasing object " + ex.ToString())
       Finally
           GC.Collect()
       End Try
   End Sub

End Class

الإسترداد من الأكسل :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim MyConnection As System.Data.OleDb.OleDbConnection
       Dim DtSet As System.Data.DataSet
       Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
       MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\vb.net-informations.xls';Extended Properties=Excel 8.0;")
       MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
       MyCommand.TableMappings.Add("Table", "Net-informations.com")
       DtSet = New System.Data.DataSet
       MyCommand.Fill(DtSet)
       DataGridView1.DataSource = DtSet.Tables(0)
       MyConnection.Close()

   End Sub
End Class

عمليات علي قواعد البيانات :

كود :
Imports System.Data.SqlClient
Public Class Form1
   Dim sCommand As SqlCommand
   Dim sAdapter As SqlDataAdapter
   Dim sBuilder As SqlCommandBuilder
   Dim sDs As DataSet
   Dim sTable As DataTable

   Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
       Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
       Dim sql As String = "SELECT * FROM Stores"
       Dim connection As New SqlConnection(connectionString)
       connection.Open()
       sCommand = New SqlCommand(sql, connection)
       sAdapter = New SqlDataAdapter(sCommand)
       sBuilder = New SqlCommandBuilder(sAdapter)
       sDs = New DataSet()
       sAdapter.Fill(sDs, "Stores")
       sTable = sDs.Tables("Stores")
       connection.Close()
       DataGridView1.DataSource = sDs.Tables("Stores")
       DataGridView1.ReadOnly = True
       save_btn.Enabled = False
       DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

   End Sub

   Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click
       DataGridView1.[ReadOnly] = False
       save_btn.Enabled = True
       new_btn.Enabled = False
       delete_btn.Enabled = False
   End Sub

   Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click
       If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
           DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
           sAdapter.Update(sTable)
       End If
   End Sub

   Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
       sAdapter.Update(sTable)
       DataGridView1.[ReadOnly] = True
       save_btn.Enabled = False
       new_btn.Enabled = True
       delete_btn.Enabled = True
   End Sub
End Class

---------------

و إلي هنا إنتهت الأكواد

يسمح بنقل الموضوع و مشاركته  Smile  Heart
الرد
#2
جزاك الله خيرا وبارك فيك
لا إله إلا الله وحده لا شريك له له الملك وله الحمد وهو على كل شئ قدير
سبحان الله وبحمده سبحان الله العظيم
سبحان الله والحمد لله ولا إله إلا الله والله أكبر ولا حول ولا قوة إلا بالله العلى العظيم
رب أغفر لي 
الرد
تم الشكر بواسطة: YousefOkasha , YousefOkasha , elgokr
#3
و جزاك كل خير
الرد
تم الشكر بواسطة: elgokr
#4
السلام عليكم

الله يعطيك الصحة والعافية
  Heart   الرجاء البحث قبل وضع السؤال  Heart

   كل عام وانتم بالف بخير.  
الرد
تم الشكر بواسطة: YousefOkasha , YousefOkasha , elgokr


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  [كود] طريقة ملئ DATAGRIDVIEW بقواعد البيانات الموجودة على السيرفر galas 0 249 13-08-18, 04:17 PM
آخر رد: galas
  جمع قيم عمود محدد لأي من DataGridView و LisBox و ListView و DataTable @@أبورائد@@ 8 6,678 11-03-15, 04:27 AM
آخر رد: ابو نجلاء

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم