DataGridView Sütunundaki Verileri Kritere Bağlı Sıralama
Bu örneğimizde Sql veritabanımızdaki verileri DataGridView nesnemizde gösterip, belli kriterlere göre verimizi sıralayarak göstereceğiz. İlk önce sql veritabanınızı oluşturun. Bu örneğimizde veritabanımızın ismi bilgiler.mdf, table ın ismi kisibilgi.
Formunuzu aşağıda Şekil1 de gösterildiği gibi tasarlayın.
Forma yazılacak kodlar:
Public Class Form1
Private Sub btnSirala_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSirala.Click
If cmb.Text = "" Or cmb.Text = "Sıralama Kriteri Seçiniz..." Then
MsgBox("Sıralama Kriteri Seçiniz...", MsgBoxStyle.OkOnly, "Bahadirsa Hata")
Exit Sub
End If
Dim str As String
Select Case cmb.SelectedIndex
Case 0
str = "Select * From kisibilgi order by " & cmb.Text.Substring(0, 2)
Case 1
str = "Select * From kisibilgi order by " & cmb.Text.Substring(0, 5)
Case 2
str = "Select * From kisibilgi order by " & cmb.Text.Substring(0, 6) & " " & "ASC"
Case 3
str = "Select * From kisibilgi order by " & cmb.Text.Substring(0, 6) & " " & "DESC"
Case 4
str = "Select * From kisibilgi order by " & cmb.Text.Substring(0, 2)
End Select
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(str, baglan)
Dim CB As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(adp)
baglan.Open()
Dim ds As New DataSet()
adp.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
baglan.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmb.Text = "Sıralama Kriteri Seçiniz..."
With Me
.Text = "Sütun Sıralama...Bahadirsa"
.MaximizeBox = False
.Location = New Point(200, 200)
End With
Dim columnHeaderStyle As New DataGridViewCellStyle
columnHeaderStyle.BackColor = Color.Aqua
columnHeaderStyle.Font = New Font("Tahoma", 12, FontStyle.Bold)
DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
DataGridView1.BackgroundColor = Color.AliceBlue
With Me.DataGridView1.RowTemplate
.DefaultCellStyle.BackColor = Color.LightYellow
.DefaultCellStyle.ForeColor = Color.Navy
.DefaultCellStyle.Font = New Font("Tahoma", 12, FontStyle.Regular)
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 ds As New DataSet()
adp.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
Dim i As Integer
For i = 0 To ds.Tables.Count - 1
If i = 0 Then
DataGridView1.Columns(i).Width = 70
Else
DataGridView1.Columns(i).Width = 120
End If
Next
baglan.Close()
End Sub
End Class
Bir sonraki makalede görüşmek üzere...Bahadirsa