In this tutorial, you will learn how to drop-down an arbitrary control from a grid cell. This example uses a ListBox control containing pre-defined input values in order to facilitate user data entry. The list will drop down whenever the user initiates editing, such as by clicking the current cell. You will also learn how to place a button in the cell which, when clicked, will cause the ListBox control to appear. You can drop-down any control from a grid cell using techniques similar to those described in this tutorial.
Complete the following steps:
The CustType field in the second column (Column1) of the grid displays numeric values ranging from 1 through 5, which represent the following customer types:
Drop down ListBox1, which will contain textual customer type descriptions, and allow users to double-click an item in order to enter the associated value into the grid.
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Imports System.Data Imports System.Data.Oledb Imports System.IO Imports System.Collections |
To write code in C#
C# |
Copy Code
|
---|---|
using System.Data; using System.Data.Oledb; using System.IO; using System.Collections; |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.ContactsTableAdapter.Fill(Me.DsContacts.Contacts) ' Add customer types to ListBox1. With Me.ListBox1 .Items.Add("Prospective") .Items.Add("Normal") .Items.Add("Buyer") .Items.Add("Distributor") .Items.Add("Other") .Visible = False End With ' Place a button in the CustType column. Me.C1TrueDBGrid1.Splits(0).DisplayColumns("CustType").Button = True End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void Form1_Load(System.object sender, System.EventArgs e) { this.ContactsTableAdapter.Fill(this.DsContacts.Contacts); // Add customer types to ListBox1. this.listBox1.Items.Add("Prospective"); this.listBox1.Items.Add("Normal"); this.listBox1.Items.Add("Buyer"); this.listBox1.Items.Add("Distributor"); this.listBox1.Items.Add("Other"); this.listBox1.Visible = false; // Place a button in the CustType column. this.c1TrueDBGrid1.Splits[0].DisplayColumns["CustType"].Button = true; } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub C1TrueDBGrid1_ButtonClick(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.ColEventArgs) Handles C1TrueDBGrid1.ButtonClick With ListBox1 .Left = Me.C1TrueDBGrid1.Left + Me.C1TrueDBGrid1.RecordSelectorWidth + Me.C1TrueDBGrid1.Splits(0).DisplayColumns(0).Width + Me.C1TrueDBGrid1.Splits(0).DisplayColumns(1).Width .Top = Me.C1TrueDBGrid1.Top + Me.C1TrueDBGrid1.RowTop(Me.C1TrueDBGrid1.Row) .Visible = True .Select() End With End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void c1TrueDBGrid1_ButtonClick(object sender, C1.Win.C1TrueDBGrid.ColEventArgs e) { this.listBox1.Left = this.c1TrueDBGrid1.Left + this.c1TrueDBGrid1.RecordSelectorWidth + this.c1TrueDBGrid1.Splits[0].DisplayColumns[0].Width + this.c1TrueDBGrid1.Splits[0].DisplayColumns[1].Width; this.listBox1.Top = this.c1TrueDBGrid1.Top + this.c1TrueDBGrid1.RowTop(this.c1TrueDBGrid1.Row); this.listBox1.Visible = true; this.listBox1.Select(); } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick Me.C1TrueDBGrid1.Columns("CustType").Text = Me.ListBox1.SelectedIndex + 1 Me.ListBox1.Visible = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void listBox1_DoubleClick(object sender, System.EventArgs e) { this.c1TrueDBGrid1.Columns["CustType"].Text = (this.listBox1.SelectedIndex + 1).ToString(); this.listBox1.Visible = false; } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub ListBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Leave Me.ListBox1.Visible = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void listBox1_Leave(object sender, System.EventArgs e) { this.listBox1.Visible = false; } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub C1TrueDBGrid1_Scroll(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.CancelEventArgs) Handles C1TrueDBGrid1.Scroll Me.ListBox1.Visible = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void c1TrueDBGrid1_Scroll(object sender, C1.Win.C1TrueDBGrid.CancelEventArgs e) { this.listBox1.Visible = false; } |
You've successfully completed attaching an arbitrary drop-down control to a grid cell; this concludes tutorial 9.