首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

VS.NET2005,组合控件开发,该怎么解决

2012-02-22 
VS.NET2005,组合控件开发各位C#高手,小弟一个问题请各位帮忙,在VS.NET2005中,想把Lable,TextBox/ComboBox,

VS.NET2005,组合控件开发
各位C#高手,小弟一个问题请各位帮忙,在VS.NET2005中,想把Lable,TextBox/ComboBox,Form,DataGridView,四个控件组合起来,Lable.text表示字段中文名字,在TextBox/ComboBox中输入或者点击下拉的时候能在数据库中搜索相应的信息在DataGridView中显示出来,记得有位高手(chsfly(一蓑烟雨任平生))开发过,但是代码不全,所以在这里再次提出,请不惜指教.

[解决办法]
新建一个usercontrol ,把这些东西都放进去
新建一个属性字段如 ControlText ,给Lable.text赋值
其他的照这样做就行了

TextBox/ComboBox中输入或者点击下拉的时候能在数据库中搜索相应的信息在DataGridView中显示出来,

在TextBox/ComboBox 的TextChanged 中把 DataGridView的DataSource 绑定

[解决办法]
你设置label为autozise为true。
下拉列表绑定一个bindingsource,datagridview再绑定一个bindingsource。

然后在combobox的事件中写代码就好了。
[解决办法]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RamiSaad
{
/// <summary>
/// Summary description for DataGridComboBoxColumn.
/// This class builds the combobox column user control for datagrids for windows applications.
/// It inherits DataGridTextBoxColumn, since this is already an available control in the .Net Framework.
/// What needs to be modified, depending on the application, is the Leave event.
/// To use this class, simply add it to the project, construct an instance, and use the properties of its ComboBox
/// this is a sample usage:
///
/// DataGridComboBoxColumn col2=new DataGridComboBoxColumn();
///col2.ComboBox.DataSource=dataSet.Client;
///col2.ComboBox.DisplayMember= "Name ";
///col2.ComboBox.ValueMember= "Id ";
///dataGrid1.TableStyles[0].GridColumnStyles.Add(col2);
///
/// </summary>
public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
// Hosted combobox control
private ComboBox comboBox;
private CurrencyManager cmanager;
private int iCurrentRow;

// Constructor - create combobox,
// register selection change event handler,
// register lose focus event handler
public DataGridComboBoxColumn()
{
this.cmanager = null;

// Create combobox and force DropDownList style
this.comboBox = new ComboBox();
this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
this.ComboBox.Visible = false;

// Add event handler for notification when combobox loses focus
this.comboBox.Leave += new EventHandler(comboBox_Leave);
this.ComboBox.VisibleChanged +=new EventHandler(ComboBox_VisibleChanged);
}

// Property to provide access to combobox
public ComboBox ComboBox
{
get { return comboBox; }
}

// On edit, add scroll event handler, and display combobox
protected override void Edit(System.Windows.Forms.CurrencyManager
source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly,
string instantText, bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText,
cellIsVisible);

if (!readOnly && cellIsVisible)
{
// Save current row in the DataGrid and currency manager
// associated with the data source for the DataGrid
this.iCurrentRow = rowNum;
this.cmanager = source;

// Add event handler for DataGrid scroll notification
this.DataGridTableStyle.DataGrid.Scroll
+= new EventHandler(DataGrid_Scroll);

// Site the combobox control within the current cell
this.comboBox.Parent = this.TextBox.Parent;


Rectangle rect =
this.DataGridTableStyle.DataGrid.GetCurrentCellBounds();
this.comboBox.Location = rect.Location;
this.comboBox.Size =
new Size(this.TextBox.Size.Width,
this.comboBox.Size.Height);

// Set combobox selection to given text
this.comboBox.SelectedIndex =this.comboBox.FindStringExact(this.TextBox.Text);


// Make the combobox visible and place on top textbox control
this.comboBox.Show();
this.comboBox.BringToFront();
this.comboBox.Focus();
}
}

// Given a row, get the value member associated with a row. Use the
// value member to find the associated display member by iterating
// over bound data source
protected override object
GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source,
int rowNum)
{
// Given a row number in the DataGrid, get the display member
object obj = base.GetColumnValueAtRow(source, rowNum);

// Iterate through the data source bound to the ColumnComboBox
CurrencyManager cmanager = (CurrencyManager)
(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
// Assumes the associated DataGrid is bound to a DataView or
// DataTable
DataView dataview = ((DataView)cmanager.List);

int i;

for (i = 0; i < dataview.Count; i++)
{
if (obj.Equals(dataview[i][this.comboBox.ValueMember]))
break;
}

if (i < dataview.Count)
return dataview[i][this.comboBox.DisplayMember];

return DBNull.Value;
}

// Given a row and a display member, iterate over bound data source to
// find the associated value member. Set this value member.
protected override void
SetColumnValueAtRow(System.Windows.Forms.CurrencyManager source,
int rowNum, object value)
{
try
{
object s = value;

// Iterate through the data source bound to the ColumnComboBox
CurrencyManager cmanager = (CurrencyManager)
(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
// Assumes the associated DataGrid is bound to a DataView or
// DataTable
DataView dataview = ((DataView)cmanager.List);
int i;

for (i = 0; i < dataview.Count; i++)
{
if (s.Equals(dataview[i][this.comboBox.DisplayMember]))
break;
}

// If set item was found return corresponding value,
// otherwise return DbNull.Value
if(i < dataview.Count)
s = dataview[i][this.comboBox.ValueMember];
else
s = DBNull.Value;

base.SetColumnValueAtRow(source, rowNum, s);
}
catch
{
Application.DoEvents();
}
}

// On DataGrid scroll, hide the combobox
private void DataGrid_Scroll(object sender, EventArgs e)
{
this.comboBox.Hide();
}

// On combobox losing focus, set the column value, hide the combobox,
// and unregister scroll event handler
private void comboBox_Leave(object sender, EventArgs e)
{
DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;
string s=null;
//in case the selected value is null.
try
{
if(!rowView.Row[this.comboBox.DisplayMember].GetType().FullName.Equals( "System.DBNull "))
s = (string) rowView.Row[this.comboBox.DisplayMember];
else
s= " ";
}
catch(Exception ex)


{
MessageBox.Show(ex.Message);
//s= " ";
}

SetColumnValueAtRow(this.cmanager, this.iCurrentRow, s);
Invalidate();

this.comboBox.Hide();
this.DataGridTableStyle.DataGrid.Scroll -=
new EventHandler(DataGrid_Scroll);
}

private void ComboBox_VisibleChanged(object sender, EventArgs e)
{
if(!this.ComboBox.Enabled)
{
this.ComboBox.Visible = false;
}
}

}

}
给你一段代码自己应该能看懂了!

热点排行