DataGridView da Seçili Satırı Kopyalama
Bu makalemizde DataGridView da seçili satırı diğer DataGridView a kopyalayacağız. Formunuza 2 adet DataGridView ekleyin. Aşağıdaki şekilleri inceleyin.
Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim columnHeaderStyle As New DataGridViewCellStyle
columnHeaderStyle.BackColor = Color.Aqua
columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
DataGridView2.ColumnHeadersDefaultCellStyle = columnHeaderStyle
With DataGridView1.RowTemplate
.DefaultCellStyle.BackColor = Color.LightYellow
.DefaultCellStyle.ForeColor = Color.Navy
.DefaultCellStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
.Height = 20
.MinimumHeight = 20
End With
Dim baglan As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bilgiler.mdf;Integrated Security=True;User Instance=True")
Dim adp As New SqlClient.SqlDataAdapter
adp.SelectCommand = New SqlClient.SqlCommand("Select * From kisibilgi", baglan)
Dim CB As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(adp)
baglan.Open()
Dim ds1 As New DataSet()
adp.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
baglan.Close()
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns(i).Width = 150
If i = DataGridView1.Columns.Count - 1 Then
DataGridView1.Columns(i).Width = 300
End If
Next
With Me
.MaximizeBox = False
.MinimizeBox = False
.Location = New Point(200, 200)
.Size = New System.Drawing.Size(770, 300)
.Text = "Seçili Kayıtı Diğer D.GridView da Gösterme...Bahadirsa"
.MaximizeBox = False
.MinimizeBox = False
End With
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i, j As Integer
For i = 0 To DataGridView1.RowCount - 1
Dim x As Integer = DataGridView1.CurrentRow.Index
‘DataGridView row ın olduğu yeri tanımladık.
If i = x Then
For j = 0 To DataGridView1.Rows(i).Cells.Count - 1
DataGridView1.Rows(i).Cells(j).Style.BackColor = Color.Red
‘Seçili olan row ın rengini değiştirdik.
DataGridView1.Rows(i).Cells(j).Style.ForeColor = Color.White
‘Seçili olan row ın yazı rengini değiştirdik.
Next
End If
If i <> x Then
For j = 0 To DataGridView1.Rows(i).Cells.Count - 1
DataGridView1.Rows(i).Cells(j).Style.BackColor = Color.White
DataGridView1.Rows(i).Cells(j).Style.ForeColor = Color.Black
Next
End If
Next
DataGridView1.Refresh()
End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
git()
End Sub
Sub git()
With DataGridView2.RowTemplate
.DefaultCellStyle.BackColor = Color.LightYellow
.DefaultCellStyle.ForeColor = Color.Navy
.DefaultCellStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
.Height = 20
.MinimumHeight = 20
End With
Dim baglan As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bilgiler.mdf;Integrated Security=True;User Instance=True")
Dim adp As New SqlClient.SqlDataAdapter
adp.SelectCommand = New SqlClient.SqlCommand("Select * From kisibilgi where id=‘" & DataGridView1.CurrentRow.Index + 1 & "‘", baglan)
Dim CB As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(adp)
baglan.Open()
Dim ds1 As New DataSet()
adp.Fill(ds1)
DataGridView2.DataSource = ds1.Tables(0)
DataGridView2.Refresh()
Me.Text = "Seçili Satır: " & DataGridView1.CurrentRow.Index + 1
baglan.Close()
Dim i As Integer
For i = 0 To DataGridView2.Columns.Count - 1
DataGridView2.Columns(i).Width = 150
If i = DataGridView2.Columns.Count - 1 Then
DataGridView2.Columns(i).Width = 300
End If
Next
End Sub
End Class