Click to See Complete Forum and Search --> : DataGridView populate


shers
September 25th, 2007, 01:08 AM
Hi,

I'm working on VB.NET Express and SQL Server Express. I have a DataGridView control in the form, with 8 columns, of which one column, which has default values, is hidden and one column is already filled with default values. I want to populate the DataGridView with records that exists in the database by selecting the record, based on the conditions in the SQL statement. And the record should be displayed in order of the hidden column values, without disturbing the default value column that is not hidden. How can I accomplish this? I have done some code, but it is not working. Here it is:

Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
'bolOpen = True

Dim strProj As String
strProj = LV.SelectedItems(0).Text
Dim strItem(2) As String
strItem = strProj.Split("-")

With EntryFrm
.txtRFS.Text = strItem(0)
.txtPName.Text = strItem(1)
End With

Dim strCon As String = My.Settings.ConnectString
Dim sqlConn As New SqlConnection(strCon)
Dim Cmd As SqlCommand

Dim DeptID As Integer

DeptID = cboDept.SelectedIndex.ToString + 1

Cmd = New SqlCommand("SELECT SDATE, EDATE, TLHRS, PHRS, THRS, COMP FROM PDETAILS WHERE RFS = " & strItem(0) & _
" AND DEPTID = " & DeptID & " ORDER BY PHASEID", sqlConn)

Dim DA As SqlDataAdapter
Dim DS As New DataSet

DA = New SqlDataAdapter(Cmd)
DS = New DataSet

DA.Fill(DS, "PDetails")

EntryFrm.DGV.DataSource = DS
EntryFrm.DGV.DataMember = "PDetails"

Me.Close()

End Sub

Thread1
September 25th, 2007, 01:49 AM
at first glance what can i say is that dont close the form - remove this line "Me.Close()" :D

Thread1
September 25th, 2007, 02:14 AM
erase erase :D.. i thought it was in the main form hehe.. anyway, does the column header appear in the datagridview? if any field in WHERE clause of your SELECT statement is string you must enclose it in quotes.

shers
September 25th, 2007, 02:36 AM
I got it to work after adding DataPropertyName to every column! But then, the column in which the default values were displayed, got erased and shows blank. Why?