Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown box in a datagrid? 1

Status
Not open for further replies.

jcfrasco

IS-IT--Management
Apr 27, 2001
89
0
0
US
I read a book about vb.net that showed a datagrid with a dropdown box ability in one of the fields. It was used in a web form and I would like to know if I can do this same thing with a windows program. Do I have to buy the datagrid or is there one readily available?

Thank you for any assistance in this matter!

jcfrasco
 
buy one??
Just drag a dataset from the toolbox. That'l do donkey, that'l do
[bravo] Mark
 
Zarcom,

Thanks for your response. I'm not understanding your advice very clearly! Either I'm not understanding what I'm supposed to do by dragging the dataset, or you didn't understand what I was trying to explain. What I have is a datagrid that's already bound to a dataset. What I would like is to have the functionality of one of the fields in the datagrid to be a combo box type attached to a different dataset, possibly approved employee names, that the user could select from the list. Also, I would like for the user to be able to ONLY select from the list and not be able to enter something directly. If your reply was directing me towards that then I think I need you to elaborate a little more on setting it up.

Again, I appreciate any help or advice with this problem!

jcfrasco
 
oh sorry I completely misunderstood your question.
Jus a sec I will go check see if it works......
Hmm I can't seem to see anything at the surface, however I will do some research on it. Tell me if you come up with something.

I am sure you can do this because it is possible with the ASP.NET version of the datagrid. That'l do donkey, that'l do
[bravo] Mark
 
jcfrasco,

The answer is no, this is not functionality on a Windows form, only a web form.

Craig
 
Too bad its not available on a Windows form. Can he still make dynamic text boxes and combo boxes appear like a data grid\? Would require more coding though.
 
Thanks to everyone that responded. I guess I'll try a different way of entering the data. If anyone has any additional advice I am all ears. Thanks again

jcfrasco
 
Try this one...

I have got the code somewhere from planet-source-code.com..

May this helps you.

1) Add a Datagrid to the form. Name it DataGrid1
2) Paste the code below as it is.


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = &quot;&quot;
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
Me.DataGrid1.Name = &quot;DataGrid1&quot;
Me.DataGrid1.Size = New System.Drawing.Size(584, 280)
Me.DataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(600, 301)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
Me.Name = &quot;Form1&quot;
Me.Text = &quot;Form1&quot;
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private DataTest As New DataGridTextBoxColumn()
Private WithEvents Combo As New ComboBox()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DBTable As New DataTable(&quot;Control&quot;)
Dim Strar(2) As String
Strar(0) = &quot;Name&quot;
Strar(1) = &quot;ControlCol&quot;
Strar(2) = &quot;Description&quot;
Dim i As Integer
For i = i To 2
Dim DBFD As New DataColumn(Strar(i))
DBFD.DefaultValue = &quot;&quot;
DBFD.DataType = System.Type.GetType(&quot;System.String&quot;)

DBTable.Columns.Add(DBFD)

Next
DBTable.TableName = &quot;Control&quot;

DataGrid1.DataSource = DBTable


i = 0
For i = i To 4
DBTable.LoadDataRow(Strar, True) ';
DataGrid1(i, 0) = &quot;My Name is Joe&quot;
DataGrid1(i, 1) = &quot;This is a Control Col but oh well&quot;
DataGrid1(i, 2) = &quot;Some description&quot;
Next

Dim dgdtblStyle As New DataGridTableStyle()
dgdtblStyle.MappingName = &quot;Control&quot;

dgdtblStyle.RowHeadersVisible = False ';
dgdtblStyle.HeaderBackColor = Color.LightSteelBlue ';
dgdtblStyle.AllowSorting = False ';
dgdtblStyle.HeaderBackColor = Color.Navy ';//.FromArgb(8,36,107);
dgdtblStyle.HeaderForeColor = Color.White ';
dgdtblStyle.HeaderFont = New System.Drawing.Font(&quot;Microsoft Sans Serif&quot;, 9.0F, FontStyle.Bold, GraphicsUnit.Point, 0) ';
dgdtblStyle.GridLineColor = Color.DarkGray ';
dgdtblStyle.PreferredRowHeight = Combo.Height
DataGrid1.BackgroundColor = Color.White ';


Dim Data1 As New DataGridTextBoxColumn()
Data1.MappingName = &quot;Name&quot;
Data1.HeaderText = &quot;Name&quot;
dgdtblStyle.GridColumnStyles.Add(Data1)

DataTest.MappingName = &quot;ControlCol&quot;
DataTest.HeaderText = &quot;Control&quot;
AddHandler DataTest.TextBox.Enter, AddressOf testthis
dgdtblStyle.GridColumnStyles.Add(DataTest)

Dim Data2 As New DataGridTextBoxColumn()
Data2.MappingName = &quot;description&quot;
Data2.HeaderText = &quot;Description&quot;
dgdtblStyle.GridColumnStyles.Add(Data2)

DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(dgdtblStyle)

End Sub

Private Sub testthis(ByVal Sender As Object, ByVal e As EventArgs)
Dim List As New ArrayList()
List.Add(New ListValues(&quot;HI man&quot;, &quot;1&quot;))
List.Add(New ListValues(&quot;HI man2&quot;, &quot;2&quot;))
List.Add(New ListValues(&quot;HI man3&quot;, &quot;3&quot;))
List.Add(New ListValues(&quot;HI man4&quot;, &quot;4&quot;))
List.Add(New ListValues(&quot;HI man5&quot;, &quot;5&quot;))


Combo.DataSource = List
Combo.DisplayMember = &quot;LongName&quot;
Combo.ValueMember = &quot;ShortName&quot;
DataTest.Width = Combo.Width
DataTest.TextBox.Controls.Add(Combo)
Combo.BringToFront()
End Sub

Private Sub Combo_select(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectionChangeCommitted
DataGrid1(DataGrid1.CurrentCell) = Combo.SelectedValue
End Sub
End Class

Public Class ListValues

Private myShortName As String
Private myLongName As String

Public Sub New(ByVal strlongName As String, ByVal strShortName As String)
MyBase.New()
Me.myShortName = strShortName
Me.myLongName = strlongName
End Sub

Public ReadOnly Property ShortName() As String
Get
Return myShortName
End Get
End Property

Public ReadOnly Property LongName() As String
Get
Return myLongName
End Get
End Property

Public Overrides Function ToString() As String
Return Me.ShortName & &quot; - &quot; & Me.LongName
End Function
End Class



Regards,
Rajesh and to the People at planet-source-code.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top