EDN Admin
Well-known member
hi guys,
i have a datagridview that is bound by an dataset, but due to the query if i was to include a calculated position column it would pull out more than one record per entry. so what i have done is remove the position column from the query, populate the
datagridview, then insert a position column into the datagridview based on the rows index value.
my code is thus :-
<pre class="prettyprint lang-vb Private Sub fillGrid() If DataGridView1.Columns.Count > 0 Then
DataGridView1.Columns.RemoveAt(0)
End If
Dim sConnectstring As String
Dim connection As New OleDb.OleDbConnection
sConnectstring = My.Settings.CivConnection
connection = New OleDb.OleDbConnection(sConnectstring)
Dim sqlstring As String
Select Case ComboBox1.Text
Case "All"
sqlstring = "return all the records"
Case Else
sqlstring = "return some of the records depending on the users selection"
End Select
Dim tablehold As String
Dim ds As New DataSet
Dim dataadapt As New OleDb.OleDbDataAdapter
tablehold = "blah"
dataadapt = New OleDb.OleDbDataAdapter(sqlstring, connection)
dataadapt.Fill(ds, tablehold)
DataGridView1.DataSource = ds
DataGridView1.DataMember = tablehold
DataGridView1.Columns(0).HeaderText = "Title"
DataGridView1.Columns(1).HeaderText = "Category"
DataGridView1.Columns(2).HeaderText = "Sub Category"
Dim pos As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn
pos.HeaderText = "Position"
pos.Name = "pos"
DataGridView1.Columns.Insert(0, pos)
For i As Integer = 0 To DataGridView1.RowCount - 1
DataGridView1.Rows(i).Cells(0).Value = i + 1
Next
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells End Sub [/code]
this works fine, i have my categories all ordered the way i want them to be from my query and each category now has a position of where it appears on the list.
the problem i now have is that when the user sorts the datagridview by clicking on one of the headers to make it easier to find a certain category, the position column loses the value of every cell, so the user cant see what position the categories are
in anymore.
i am toying with the idea of populating a datatable with my query and manually enter the position into that, then use the table as the source for the datagridview, but this just seems like an extra step that shouldnt be needed. would anyone have any
suggestions as to why the datagridview isnt holding the cell values i feed it, or even point me in the right direction as to how to stop this happening?
cheers for the help in advance
<br/>
View the full article
i have a datagridview that is bound by an dataset, but due to the query if i was to include a calculated position column it would pull out more than one record per entry. so what i have done is remove the position column from the query, populate the
datagridview, then insert a position column into the datagridview based on the rows index value.
my code is thus :-
<pre class="prettyprint lang-vb Private Sub fillGrid() If DataGridView1.Columns.Count > 0 Then
DataGridView1.Columns.RemoveAt(0)
End If
Dim sConnectstring As String
Dim connection As New OleDb.OleDbConnection
sConnectstring = My.Settings.CivConnection
connection = New OleDb.OleDbConnection(sConnectstring)
Dim sqlstring As String
Select Case ComboBox1.Text
Case "All"
sqlstring = "return all the records"
Case Else
sqlstring = "return some of the records depending on the users selection"
End Select
Dim tablehold As String
Dim ds As New DataSet
Dim dataadapt As New OleDb.OleDbDataAdapter
tablehold = "blah"
dataadapt = New OleDb.OleDbDataAdapter(sqlstring, connection)
dataadapt.Fill(ds, tablehold)
DataGridView1.DataSource = ds
DataGridView1.DataMember = tablehold
DataGridView1.Columns(0).HeaderText = "Title"
DataGridView1.Columns(1).HeaderText = "Category"
DataGridView1.Columns(2).HeaderText = "Sub Category"
Dim pos As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn
pos.HeaderText = "Position"
pos.Name = "pos"
DataGridView1.Columns.Insert(0, pos)
For i As Integer = 0 To DataGridView1.RowCount - 1
DataGridView1.Rows(i).Cells(0).Value = i + 1
Next
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells End Sub [/code]
this works fine, i have my categories all ordered the way i want them to be from my query and each category now has a position of where it appears on the list.
the problem i now have is that when the user sorts the datagridview by clicking on one of the headers to make it easier to find a certain category, the position column loses the value of every cell, so the user cant see what position the categories are
in anymore.
i am toying with the idea of populating a datatable with my query and manually enter the position into that, then use the table as the source for the datagridview, but this just seems like an extra step that shouldnt be needed. would anyone have any
suggestions as to why the datagridview isnt holding the cell values i feed it, or even point me in the right direction as to how to stop this happening?
cheers for the help in advance
<br/>
View the full article