Home All Groups Group Topic Archive Search About

datagrid filled from a list

Author
5 Apr 2005 7:53 AM
Sam
Hi,
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
dataset.
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 ?
Thx

Author
5 Apr 2005 8:33 AM
Cor Ligthert
Sam,

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.

Cor
Author
5 Apr 2005 8:41 AM
Sam
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 ?
thx
Author
5 Apr 2005 8:55 AM
Cor Ligthert
Sam,

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.

Cor
Author
5 Apr 2005 9:11 AM
Sam
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
thx
Author
5 Apr 2005 10:25 AM
Cor Ligthert
Sam,

I think I got it as you wanted it.
See here the sample, needs only a form and a datagrid and than pasting this
in.
(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.Columns.Add("Name")
        dt.Columns.Add("Country")
        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",
"Germany"}
        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
        ts.GridColumnStyles.Add(textCol)
        Dim cmbTxtCol As New DataGridComboBoxColumn(mylist, dt)
        cmbTxtCol.MappingName = "Country"
        cmbTxtCol.HeaderText = "Countries"
        cmbTxtCol.Width = 100
        ts.GridColumnStyles.Add(cmbTxtCol)
        ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
        cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
        DataGrid1.TableStyles.Add(ts)
    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)
        MyBase.New()
        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,
cellIsVisible)
        Me.ColumnComboBox.Items.Clear()
        For Each str As String In mylist
            Dim dt() As DataRow = myDatatable.Select("Country = '" & str &
"'")
            If dt.Length = 0 Then
                ColumnComboBox.Items.Add(str)
            End If
        Next
        ColumnComboBox.Items.Add(Me.TextBox.Text)
        mRowNum = rowNum
        cmSource = source
        ColumnComboBox.Parent = Me.TextBox.Parent
        ColumnComboBox.Location = Me.TextBox.Location
        ColumnComboBox.Size = New Size(Me.TextBox.Size.Width,
ColumnComboBox.Size.Height)
        ColumnComboBox.Text = Me.TextBox.Text
        TextBox.Visible = False
        ColumnComboBox.Visible = True
        ColumnComboBox.BringToFront()
        ColumnComboBox.Focus()
    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
EventArgs)
        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
            Invalidate()
        End If
        ColumnComboBox.Hide()
    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
            MyBase.WndProc(m)
        End If
    End Sub
End Class
///

I hope this helps,

Cor
Author
5 Apr 2005 11:52 AM
Sam
thx
your example is great.
I'll probably use it for starting
Author
5 Apr 2005 2:15 PM
Sam
Cor,

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 ?

Thx
Author
5 Apr 2005 2:41 PM
Cor Ligthert
Sam,

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

Cor
Author
5 Apr 2005 2:53 PM
Sam
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 ?
Author
5 Apr 2005 3:35 PM
Cor Ligthert
Sam,

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

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.

:-)

Cor
Author
5 Apr 2005 4:08 PM
Sam
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...:(
Author
5 Apr 2005 10:04 AM
Sam
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 ?