Let combobox also set the data table

Source: Internet
Author: User
Is there no data source with no table head in Combobox? Do you want to create a data source with a table head? Follow me! The most effective features are as follows ﹕

In order to realize the above effect, let us implement it step by step ﹕
Step 1:
Start VS. net2003 to create a Windows program and change the automatically generated Form1 to FrmTest. This step is easier than simply writing a ticket. Next
Step 2:
Add a new WinForm named "frmDropDownDetail". This is the Form tool we will use to fill data centers. Define a DataGrid and make it public. The Configuration window is not displayed in any business, so that the operation will not be unpleasant in any business. Private DataGrid dataGrid;
Public DataGrid
{
Get {return dataGrid ;}
Set {dataGrid = value ;}
}
Private int index;
Public int Index
{
Get {return index ;}
Set {index = value ;}
}
Private int mywidth;
Public int MyWidth
{
Get {return mywidth ;}
Set {mywidth = value ;}
}

With such attention, I need to explain the role of the above attention separately. the DataGrid does not need to be said. It is clear at first glance that it is used to handle data volumes. Index returns the value of the nth column when we upload the dataGrid. MyWidth is the Width of the WinForm returned.
Add the following representatives to the structure function ﹕ This. FormBorderStyle = FormBorderStyle. None;
This. StartPosition = FormStartPosition. Manual;
This. ShowInTaskbar = false;
DataGrid = new DataGrid ();
This. Controls. Add (dataGrid );
DataGrid. Dock = DockStyle. Fill;
DataGrid. CaptionVisible = false;
DataGrid. PreferredColumnWidth = 75;
DataGrid. ReadOnly = true;
DataGrid. MouseUp + = new MouseEventHandler (dataGrid_MouseUp );

Of course, you can also directly design the program in the design segment to allow you to use the design tool to automatically generate this segment. Naturally, you can see the following code for the MouseUp event ﹕ Private void dataGrid_MouseUp (object sender, MouseEventArgs e)
{
System. Drawing. Point pt = new System. Drawing. Point (e. X, e. Y );
DataGrid. HitTestInfo hti = (sender as DataGrid). HitTest (pt );
If (hti. Type = DataGrid. HitTestType. Cell)
{
(Sender as DataGrid). Select (hti. Row );
This. index = hti. Column;
}
}

The above completes a Winform that can be added to the lower volume of combobox. Next we will use zookeeper.
Step 3:
Return to the FrmTest of zookeeper. Open the parameter generator and define the following Variable constant ﹕ Private System. Windows. Forms. ComboBox cbxSource;
Private System. Windows. Forms. Label lblSource;
Private SqlConnection sqlConn = null;
Private SqlDataAdapter sqlDa = null;
Private DataSet ds = null;
Private frmDropDownDetail frm;
Const int WM_KEYUP = 0x101;
Const int Index = 0;

Add a Load event ﹕ Try
{
Frm = new frmDropDownDetail ();
Frm. Activated + = new EventHandler (frm_Activated );
Frm. Deactivate + = new EventHandler (frm_Deactivate );
Frm. DataGrid. Click + = new EventHandler (DataGrid_Click );
SqlConn = new SqlConnection ("workstation id = 'kingna'; packet size = 4096; user id = sa; password =; data source = 'localhost'; persist security info = False; initial catalog = NorthWind ");
String strSql = "SELECT ProductID AS [product catalog], ProductName AS [product catalog], QuantityPerUnit AS [product catalog], unitPrice AS [single region] FROM Products ";
SqlDa = new SqlDataAdapter ();
Ds = new DataSet ();
SqlDa. SelectCommand = new SqlCommand (strSql, sqlConn );
SqlDa. Fill (ds, "Products ");
Frm. DataGrid. DataSource = ds; // you can specify the resource source for the drop-down list.
Frm. DataGrid. DataMember = "Products ";
}
Catch (Exception E) {MessageBox. Show (E. ToString ());}

The generation of custom events is as follows ﹕ Protected override void WndProc (ref Message m)
{
If (m. Msg = WM_KEYUP)
Return;
Base. WndProc (ref m );
}
Private void frm_Activated (object sender, EventArgs e)
{
Frm. Width = this. cbxSource. Width;
}
Private void frm_Deactivate (object sender, EventArgs e)
{
Frm. Hide ();
}

Private void DataGrid_Click (object sender, EventArgs e)
{
BindingManagerBase cm = frm. BindingContext [frm. DataGrid. DataSource, frm. DataGrid. DataMember];
This. cbxSource. Text = (System. Data. DataRowView) cm. Current) [Index]. ToString ();
Frm. Hide ();
}

Private void cbx_DropDown (object sender, System. EventArgs e)
{
Frm. Left = cbxSource. PointToScreen (new Point (0, cbxSource. Height). X;
Frm. Top = cbxSource. PointToScreen (new Point (0, cbxSource. Height). Y;
Frm. MyWidth = (sender as ComboBox). Width;
Frm. Show ();
Frm. BringToFront ();
}

To solve this problem, WM_KEYUP is used to block the frmDropDownDetail window that is used when the kernel is used.

So far, our function of adding a dynamic data table to combobox is basically realized! You can expand it on your own. If you need a friend from the source region, please use jinliangliu # 163.com with me. If you have a better friend, please kindly advise!
Of course, dudu thought that it was not suitable for the first place. Please try to remove it! There is not much skill in this article!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.