Here is the full code
Code:Public Function DGVload_testsql() As DataView Dim con = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Danial\documents\visual studio 2010\Projects\ESI_PF_Payroll_V1\ESI_PF_Payroll_V1\Pay.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True") con.Open() '_string = "select * from Employee inner join Employer on Employee.IDEmployer = Employer.EmployerID" _string = "select Employer.CompanyName,Employee.Firstname from Employee inner join Employer on Employee.IDEmployer = Employer.EmployerID" Dim SampleSource As New DataSet Dim TableView As DataView Try Dim SampleCommand As New SqlCommand() Dim SampleDataAdapter = New SqlDataAdapter() SampleCommand.CommandText = _string SampleCommand.Connection = con SampleDataAdapter.SelectCommand = SampleCommand SampleDataAdapter.Fill(SampleSource) TableView = SampleSource.Tables(0).DefaultView 'Use colors in DataGridView Test_dgv_sql.GridColor = Color.Red Test_dgv_sql.CellBorderStyle = DataGridViewCellBorderStyle.None Test_dgv_sql.BackgroundColor = Color.LightGray Test_dgv_sql.DefaultCellStyle.SelectionBackColor = Color.Brown 'DGV1.DefaultCellStyle.SelectionForeColor = Color.Yellow Test_dgv_sql.DefaultCellStyle.WrapMode = DataGridViewTriState.[True] Test_dgv_sql.SelectionMode = DataGridViewSelectionMode.FullRowSelect Test_dgv_sql.AllowUserToResizeColumns = False Test_dgv_sql.RowsDefaultCellStyle.BackColor = Color.Bisque Test_dgv_sql.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige Test_dgv_sql.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkRed Test_dgv_sql.ColumnHeadersDefaultCellStyle.ForeColor = Color.DarkGray 'cells and rows height in DataGridView Dim row As DataGridViewRow = Me.Test_dgv_sql.RowTemplate row.DefaultCellStyle.BackColor = Color.Bisque row.Height = 30 row.MinimumHeight = 15 Catch ex As Exception Throw End Try Return TableView End Function End ClassCode:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Test_dgv_sql.DataSource = DGVload_testsql() End Sub




Reply With Quote