2011-09-29 54 views
0

我有一个用户控件(看截图): enter image description here数据绑定到一个usercontrol

我想知道我怎么能数据绑定到该控件。

1)我想&降这个用户控件拖动到形式 2)I愿决定应该使用哪个表从我的数据库 3)数据网格(在底部的灰控制)应该自动绑定到datasource指向我希望使用的表格 4)usercontrol中的详细Tab应该自动填充标签+ textbox,用于我希望使用的表格的每个字段

我的问题是..我该如何实现这个?有任何想法吗?

迎接

+0

您的问题是否适用于运行时间或设计时间?为了达到这个目的,你尝试了什么? –

+0

它适用于设计时间 – darkdog

回答

3

1)当你建立你的项目,你的用户控件显示在VisualStudio的工具箱,所以你可以拖动&,无论你想它拖放到你的形式。

2)在你的用户控件的某些属性暴露你的DataGridView的数据源,例如:

public BindingSource MyDataGridViewDataSource 
{ 
    get 
    { 
     return MyDataGridView.DataSource; // or you can skip 'get' if you don't need it 
    } 
    set 
    { 
     MyDataGridView.DataSource = value; 
    } 
} 

3)...或者,如果你想列名,使用属性,获取/设置的DataTable(记得第一填充它http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx):

private DataTable _myDataTable = new DataTable(); 
public DataTable MyDataTable 
{ 
    get 
    { 
    return _myDataTable; 
    } 
    set 
    { 
    _myDataTable = value; 
    BindingSource bs = new BindingSource(); 
    bindingSource1.DataSource = value; 
    MyDataGridView.DataSource = bs; 

    // 4) fill your labels somewhere here 
    string tablename = value.TableName; 
    foreach (DataColumn col in value.Columns) 
    Console.WriteLine("{0}\t{1}", col.ColumnName, col.DataType); 
    } 
} 

这不是一个完整的解决方案,但它是一个起点。

+0

这是一个很好的解决方案。谢谢阿里!当然,它不是一个完整的解决方案,但它是我所需要的。你帮了我很多。 – darkdog

相关问题