Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 10th, 2003, 06:33 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
Default Drop Down within a datagrid

I always used sheridan datagrids with VB 6, which enabled a dropdown within a datagrid that you could link to a different dataset, other then the one the datagrid is linked too. Can you do this in VB.Net and if so how?

Thanks in advance

Louisa

 
Old December 10th, 2003, 03:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

I believe this is possible. However, I haven't tried this yet. One thing that I have done is to build a dataset that contains multiple tables.

When this is coded, the datagrid will start with a base table and have plus/minus signs at the left. When the plus sign is clicked, it expands to show a link to the other table. When the link is clicked, the datagrid is populated with the other table.

You need to fill your dataset with the different tables. Note that they will have to have a common field. Then create a relation between those two tables and bind it to your datagrid.

Here is a snippet of code that should help you get started:

Dim ds As DataSet = New DataSet

Dim cmdOne As SqlCommand = connSQL.CreateCommand
cmdOne.CommandType = CommandType.Text
cmdOne.CommandText = "SELECT * FROM Table1"
Dim daOne As SqlDataAdapter = New SqlDataAdapter
daOne.SelectCommand = cmdOne
daOne.Fill(ds, "tblOne")

Dim cmdTwo As SqlCommand = connSQL.CreateCommand
cmdTwo.CommandType = CommandType.Text
cmdTwo.CommandText = "SELECT * FROM Table2"
Dim daTwo As SqlDataAdapter = New SqlDataAdapter
daTwo.SelectCommand = cmdTwo
daTwo.Fill(ds, "tblTwo")

Dim relTables As DataRelation = _
ds.Relations.Add("Relation_Name", ds.Tables("tblOne").Columns("pri_key"), ds.Tables("tblTwo").Columns("foreign_key"))

dg.DataSource = ds
dg.DataMember = "tblOne"

----------------------------------

You will have to import the necessary namespaces. This example uses SQL Server, but you should be able to modify this pretty easily to use with other databases.

J
 
Old December 11th, 2003, 12:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Louisa,

I just found this link. I haven't had time to look at it, but it might contain examples of what you are looking for.

http://www.planet-source-code.com/vb...=225&lngWId=10

J
 
Old December 15th, 2003, 10:02 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks I have looked at the link, and it is very helpful, except :o( it shows how to add a combo box to columns that you have added in code not using a dataadapter etc to the grid as I have done. Is this the only way of doing it?

Thank you very much
Louisa

 
Old April 21st, 2005, 09:54 AM
Registered User
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gruffta
Default

Hi Louisa,

I was looking for a similair sort of solution myself and was pleasently surprised how easy it was to implement.

I have to columns in my datagrid : itemType and itemCategory which I wanted both to have a combo.

First of declare variables for combobox before any procedures ie:

    Private WithEvents ItemTypeComboBox As New ComboBox
    Private WithEvents ItemCategoryComboBox As New ComboBox

now have a look at your grid column styles property and note the names of your columns. go back to code view and expand the "windows form designer generated code" and in the sub New() add after the Initlizecomponent call add your handlers, don't worry if intellisense doesn't pick up your columns it still works. for example (all 1 line)

   AddHandler ItemTypeTextBox.TextBox.Enter, AddressOf CreateDropDownItemType

   AddHandler ItemCategory.TextBox.Enter, AddressOf CreateDropDownItemCategory

where ItemTypeTextBox and ItemCategory are the name of your columns, the CreateDropDownItemType and CreateDropDownItemCategory after the address of are sub procedures to execute when you enter the textbox.

You must declare these procedures with parameters:
  sender as object, e as system.eventargs

in these procedures you would tell the combobox where your datasource is and set the widths and add the control to the textbox, for example :

Private Sub CreateDropDownItemType(ByVal sender As Object, ByVal e As System.EventArgs)

        Me.ItemTypeComboBox.DataSource = datasource
        Me.ItemTypeComboBox.DisplayMember = displaymember
        Me.ItemTypeComboBox.ValueMember = valuemember
        ItemTypeComboBox.Width = ItemTypeTextBox.Width
        ItemTypeTextBox.TextBox.Controls.Add(ItemTypeCombo Box)

    End Sub

    Private Sub CreateDropDownItemCategory(ByVal sender As Object, ByVal e As System.EventArgs)

        Me.ItemCategoryComboBox.DataSource = datasource
        Me.ItemCategoryComboBox.DisplayMember = displaymember
        Me.ItemCategoryComboBox.ValueMember = valuemember
        Me.ItemCategoryComboBox.Width = ItemCategory.Width
        Me.ItemCategory.TextBox.Controls.Add(ItemCategoryC omboBox)

    End Sub

and there you have it, adding handlers in this way should work for most controls, but i haven't tested it yet but I don't see why you wouldn't be able to add handlers to add a date time picker when you went to a textbox.

you would also have to add a event handler for the combobox selectionchanged event to update datagrid.currentCell value.

Just looked at the date, probably no use for you any more but maybe someone else will find this useful.

Gareth


-- WHEN FREEDOM IS OUTLAWED, ONLY THE OUTLAWS WILL BE FREE





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to drag data from datagrid and drop to folder parveen_kumar24@rediffmai .NET Framework 2.0 0 August 30th, 2006 09:54 AM
Drop down list in datagrid help jmd ASP.NET 1.0 and 1.1 Basics 0 January 8th, 2005 04:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.