PC Review


Reply
Thread Tools Rate Thread

datagrid filled from a list

 
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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

 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Apr 2005
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


 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Apr 2005
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


 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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

 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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 ?

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Apr 2005
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



 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
thx
your example is great.
I'll probably use it for starting

 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Apr 2005
Sam,

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

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I update datagrid while table is being filled?? VM Microsoft C# .NET 1 24th Jun 2004 06:44 PM
Displaying datatable in datagrid while table's being filled... VM Microsoft C# .NET 2 27th May 2004 06:00 PM
RE: bind datagrid to dataset filled at runtime =?Utf-8?B?QmluIFNvbmcsIE1DUA==?= Microsoft ASP .NET 0 8th Apr 2004 09:06 PM
Re: How to let columns filled into Datagrid? Eric Cadwell Microsoft C# .NET 6 22nd Oct 2003 09:52 PM
How to let columns filled into Datagrid? chrisben Microsoft C# .NET 0 21st Oct 2003 09:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:01 AM.