By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,582 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,582 IT Pros & Developers. It's quick & easy.

datagrid filled from a list

P: n/a
Do you know where I can find a simple example of what I'm trying to
achieve :
I have a dataset filled with two columns from a table of my database.
Then the dataset is used to fill a datagrid.
But I would like to store the data of the dataset in a list. My dataset
has 1 column of integer values and 1 column of string values, so my
list should have the same.
Then the datagrid should be filled using the list instead of the
I don't know :
-how to create a list and store my dataset's values
-how to specify a list as the datasource for my datagrid

can you help ?

Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies

P: n/a

I have seen on first sight, crazy questions, yours is one on the top.

However probably you have a reason for it that I don't know, can you explain
that to me, because it makes me very curious and maybe there is a much
simpler solution.

Nov 21 '05 #2

P: n/a
yes it sounds crazy...
My problem is that I have comboboxes in my datagrid. When the user
selects a value in one combobox, then the comboboxes on the rows below
should not contain that value any more. So basically the datasource is
getting updated for the row below. In my opinion this is impossible to
achieve as you can have only one datasource per datagrid (and not per
row). Therefore I'd like to populate my comboboxes dynamically from a
list that I would update accordingly ....
Is this possible ? Have you seen similar stuff before ?

Nov 21 '05 #3

P: n/a

I believe that you will not succeed logical in this. See this
1 2
2 3
3 4

You change row 1 the 2 in 3 what should than happen in row 2.
(While row 2 is actually not even in action so there happens nothing).

This will be a very lonly route to go in my opinon.

Nov 21 '05 #4

P: n/a
If you have a better idea, let me know !
Indeed it's going to be a long route to go but i'm short in time, so if
there's a better way to do that... give me a shot

Nov 21 '05 #5

P: n/a
In fact, you should not be able to change the 2 in 3 to row 1. Sorry
for my misunderstanding. When you select a value in a combobox, this
value should disappear from all the other combobox of this column.
Is it clearer ?

Nov 21 '05 #6

P: n/a

I think I got it as you wanted it.
See here the sample, needs only a form and a datagrid and than pasting this
(at the end you will than have to remove a end class and something)
It is of course a little bit rougly done, however I tried it.

Private dt As New DataTable("Names")
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
dt.LoadDataRow(New Object() {"Cor", "Holland"}, True)
dt.LoadDataRow(New Object() {"Ken", "Florida"}, True)
dt.LoadDataRow(New Object() {"Terry", "England"}, True)
Dim mylist As String() = {"Holland", "Florida", "England",
Dim dv As New DataView(dt)
dv.AllowNew = False
DataGrid1.DataSource = dv
Dim ts As New DataGridTableStyle
ts.MappingName = "Names"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
Dim cmbTxtCol As New DataGridComboBoxColumn(mylist, dt)
cmbTxtCol.MappingName = "Country"
cmbTxtCol.HeaderText = "Countries"
cmbTxtCol.Width = 100
ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
'make dataset
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

End Sub
End Class
' The simple DatagridCombobox
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Public WithEvents ColumnComboBox As NoKeyUpCombo 'special class
Private WithEvents cmSource As CurrencyManager
Private mRowNum As Integer
Private isEditing As Boolean
Private mylist As Object()
Private myDatatable As DataTable
Public Sub New(ByVal list As String(), ByVal dt As DataTable)
mylist = list
myDatatable = dt
ColumnComboBox = New NoKeyUpCombo
AddHandler ColumnComboBox.SelectionChangeCommitted, _
New EventHandler(AddressOf ComboStartEditing)
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager,
ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As
Boolean, _
ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText,
For Each str As String In mylist
Dim dt() As DataRow = myDatatable.Select("Country = '" & str &
If dt.Length = 0 Then
End If
mRowNum = rowNum
cmSource = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width,
ColumnComboBox.Text = Me.TextBox.Text
TextBox.Visible = False
ColumnComboBox.Visible = True
End Sub
Protected Overloads Overrides Function Commit(ByVal dataSource As _
CurrencyManager, ByVal rowNum As Integer) As Boolean
If isEditing Then
isEditing = False
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
End If
Return True
End Function
Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As
isEditing = True
MyBase.ColumnStartedEditing(DirectCast(sender, Control))
End Sub
Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
Handles ColumnComboBox.Leave
If isEditing Then
SetColumnValueAtRow(cmSource, mRowNum, ColumnComboBox.Text)
isEditing = False
End If
End Sub
End Class
Public Class NoKeyUpCombo
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg <> &H101 Then
End If
End Sub
End Class

I hope this helps,


Nov 21 '05 #7

P: n/a
your example is great.
I'll probably use it for starting

Nov 21 '05 #8

P: n/a

In the code you sent me, your datagrid combo box inherits from
DataGridTextBoxColumn, wheras mine inherits from combobox. Then how can
I use your code as it overides the Edit method ?


Nov 21 '05 #9

P: n/a

All is in the sample, so why not use that, just copy and paste you know.

Nov 21 '05 #10

P: n/a
yes that'd be great :)
but unfortunately I'd like to be able to do more complex things with my
datagrid. For instance a combobox field could receive records from a
table with the displaymember set to one of the field and the
valuemember set to the primary key. It seems to me that it's not
possible with this code, is it ?

Nov 21 '05 #11

P: n/a

When you had set that in advance than I had tried it with that one.

I did want to keep it simple for you and took the one I showed.

I think that now the chalenge is for you to see what I did with this one.

When you did not succeed in some days, maybe than I can try it with the
other one.


Nov 21 '05 #12

P: n/a
LOL :)
I don't have some days unfortunately, I have 1 day !
Anyway, I'm getting to it but it's f**ing hard for me...:(

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.