如何在DataGridViewComboBoxColumn中使用多个数据源? [英] How to use multiple datasources in DataGridViewComboBoxColumn?

查看:70
本文介绍了如何在DataGridViewComboBoxColumn中使用多个数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个win表单应用程序中,我正在加载一个名为dg的datagridview,这个数据来自一个名为servRet的数据源。

servRet填入数据网格。在将dg.datasource设置为servRet.value之后,我正在创建一个类型为DataGridViewComboBoxColumn的新列,它将来可以容纳硬币类型(例如:CAD,US,NIS ......)。

现在,这是一个棘手的部分:

组合框假设从servRet显示displayValue但是当客户端打开组合框时,他需要看到所有的硬币列表。

硬币列表是从另一个名为servRet1的数据源中拖出的。

我试图完成组合框显示当前当前值的状态在servRet中行,同时用第二个数据源填充组合项。

不幸的是,我遇到了几个问题:

1.我试过将servRet1传递给列表并将其加载到组合但我无法将项目添加到绑定的组合框。

2.我无法将2个数据源加载到一个组合框。

3.即使将servRet加载到组合框中并且在拖动打开时我将组合数据源设置为null并加载servRet1,在selectedIndexChange上,它不允许我选择任何其他值而不是然后它持有的第一个值。

这里是t他加载第一个数据的代码:

  public   void  FillData(IShaarimService服务)
{
List< string> listTeuraMatbea = new List< string>();
List< string> listKodAndShemMatbea = new List< string>();
var servRet = service.getNiyarotBeMatbeaShoneMeBank();
var servRet1 = service.getMatbeaot();

if (ServiceUtil.IsValid(servRet1))
{

foreach (DataRow row in servRet1.Value.Rows)
{
listKodAndShemMatbea.Add(row [& QUOT; KodAndShemMatbea&安培; QUOT]的ToString());
listTeuraMatbea.Add(row [& quot; TeuraMatbea& quot;]。ToString());
}
}

if (ServiceUtil.IsValid(servRet))
{
grdNiyar.SetDataSourceByServerReturn(servRet);
dg.AutoGenerateColumns = false ;
dg.ColumnCount = 4 ;
dg.DataSource = servRet.Value;
DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
cb.DataSource = servRet.Value.DefaultView.ToTable( true ,KodAndShemMatbea&);
// foreach(listKodAndShemMatbea中的var项目)
// {
// cb.Items.Add(item);
// }
cb.HeaderText =& quot;מטבעבבנק& quot ;;
cb.DisplayMember =& quot; 123& quot ;;
cb.DataPropertyName =& quot; KodAndShemMatbea& quot ;;
cb.DisplayMember =& quot; KodAndShemMatbea& quot ;;
cb.ValueMember =& quot; KodAndShemMatbea& quot ;;
dg.Columns.Add(cb);

} < / pre >





我们非常感谢任何帮助!!!

解决方案

我认为这个MSDN示例将帮助您转发

DataGridViewComboBoxColumn.DataSource Property [ ^ ]



基本上就是这样的。

1.您有一个 DataGridView ,其中您将DataSource设置为 DataTable BindingSource

2.您指定其中一列应为a DataGridViewComboBoxColumn

此列有一个名为 DataPropertyName 的属性,该属性设置为DataTable。



来自设计师模块的代码

  this  .dataSet1 =  new  System.Data.DataSet(); 
this .dataTable1 = new System.Data.DataTable();
this .dataColumn1 = new System.Data.DataColumn();
this .dataColumn2 = new System.Data.DataColumn();
this .bindingSource3 = new System.Windows.Forms.BindingSource( .components);
this .columnSeasons = new System.Windows.Forms.DataGridViewComboBoxColumn();

this .dataGridView1.DataSource = this .bindingSource3;

//
// dataSet1
//
.dataSet1.DataSetName = NewDataSet;
.dataSet1.Tables.AddRange( new System.Data.DataTable [] {
this .dataTable1});
//
// dataTable1
//
this .dataTable1.Columns.AddRange( new System.Data.DataColumn [ ] {
this .dataColumn1,
this .dataColumn2});
this .dataTable1.TableName = Table1 ;
//
// dataColumn1
//
.dataColumn1.ColumnName = column1的;
//
// dataColumn2
//
.dataColumn2.ColumnName = 列2;
//
// bindingSource3
//
this .bindingSource3.DataMember = 表1;
this .bindingSource3.DataSource = this .dataSet1;
//
// columnSeasons
//
this .columnSeasons.DataPropertyName = column1的; // 这来自DataSet
this .columnSeasons.HeaderText = Seasons;
.columnSeasons.Name = columnSeasons ;
this .columnSeasons.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this .columnSeasons.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;







3.现在需要为组合框的下拉列表设置数据源。

这可以是字符串列表或数据表。

 DataTable dtSeasons =  new  DataTable(  季节); 
dtSeasons.Columns.Add( Id typeof int ));
dtSeasons.Columns.Add( Value typeof string ));

dtSeasons.Rows.Add( 1 );
dtSeasons.Rows.Add( 2 );
dtSeasons.Rows.Add( 3 );
dtSeasons.Rows.Add( 4 );

columnSeasons.DataSource = dtSeasons;
columnSeasons.ValueMember = Id;
columnSeasons.DisplayMember = Value;





代码示例不完整,但我希望你能得到图片。


hey!
in a win forms application, i'm loading a datagridview called dg from a data source called servRet.
the servRet fills in the data grid. after setting the dg.datasource to servRet.value, i'm creating a new column of type DataGridViewComboBoxColumn which future to hold types of coins (e.g: CAD, US, NIS...).
Now, here is the tricky part:
the combo box suppose to display the displayValue from the servRet but when the client open the combo box, he needs to see all the coins list.
the coins list is dragged from another data source called servRet1.
i'm seek to accomplish a state where the combo box display the current value from the current row in the servRet and at the same time fill the item of the combo with the second data source.
unfortunately, i ran into several problems:
1. i tried to pass the servRet1 to a list and the load it to the combo but i cant add items to a binded combo box.
2. i cant load 2 data sources to one combo box.
3. even when loading the servRet to the combo box and on the drag open i'm setting the combo data source to null and load the servRet1, on the selectedIndexChange, it doesn't allow me to choose any other value rather then the first value it held.
here is the code for loading the first data:

public void FillData(IShaarimService service)
{
    List<string> listTeuraMatbea = new List<string>();
    List<string> listKodAndShemMatbea = new List<string>();
    var servRet = service.getNiyarotBeMatbeaShoneMeBank();
    var servRet1 = service.getMatbeaot();

    if (ServiceUtil.IsValid(servRet1))
    {

        foreach (DataRow row in servRet1.Value.Rows)
        {
            listKodAndShemMatbea.Add(row[&quot;KodAndShemMatbea&quot;].ToString());
            listTeuraMatbea.Add(row[&quot;TeuraMatbea&quot;].ToString());
        }
    }

    if (ServiceUtil.IsValid(servRet))
    {
        grdNiyar.SetDataSourceByServerReturn(servRet);
        dg.AutoGenerateColumns = false;
        dg.ColumnCount = 4;
        dg.DataSource = servRet.Value;
        DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
        cb.DataSource = servRet.Value.DefaultView.ToTable(true, KodAndShemMatbea&);
        //foreach (var item in listKodAndShemMatbea)
        //{
        //    cb.Items.Add(item);
        //}
        cb.HeaderText = &quot;מטבע בבנק&quot;;
        cb.DisplayMember = &quot;123&quot;;
        cb.DataPropertyName = &quot;KodAndShemMatbea&quot;;
        cb.DisplayMember = &quot;KodAndShemMatbea&quot;;
        cb.ValueMember = &quot;KodAndShemMatbea&quot;;
        dg.Columns.Add(cb);

    }</pre>



any help we be appreciate!!!

解决方案

I think this MSDN example will help you forward
DataGridViewComboBoxColumn.DataSource Property[^]

Basically it works like this.
1. You have a DataGridView where you set the DataSource to a DataTable or BindingSource.
2. You specify that one of the columns should be a DataGridViewComboBoxColumn
This column has a property called DataPropertyName which is set to a column in the DataTable.

Code from the designer module

this.dataSet1 = new System.Data.DataSet();
this.dataTable1 = new System.Data.DataTable();
this.dataColumn1 = new System.Data.DataColumn();
this.dataColumn2 = new System.Data.DataColumn();
this.bindingSource3 = new System.Windows.Forms.BindingSource(this.components);
this.columnSeasons = new System.Windows.Forms.DataGridViewComboBoxColumn();

this.dataGridView1.DataSource = this.bindingSource3;

// 
// dataSet1
// 
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
this.dataTable1});
// 
// dataTable1
// 
this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2});
this.dataTable1.TableName = "Table1";
// 
// dataColumn1
// 
this.dataColumn1.ColumnName = "Column1";
// 
// dataColumn2
// 
this.dataColumn2.ColumnName = "Column2";
// 
// bindingSource3
// 
this.bindingSource3.DataMember = "Table1";
this.bindingSource3.DataSource = this.dataSet1;
// 
// columnSeasons
// 
this.columnSeasons.DataPropertyName = "Column1"; // This comes from the DataSet
this.columnSeasons.HeaderText = "Seasons";
this.columnSeasons.Name = "columnSeasons";
this.columnSeasons.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.columnSeasons.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;




3. Now you need to set the data source for the dropdown list of the combo box.
This can be a list of strings or a data table.

DataTable dtSeasons = new DataTable("Seasons");
dtSeasons.Columns.Add("Id", typeof(int));
dtSeasons.Columns.Add("Value", typeof(string));

dtSeasons.Rows.Add(1, "Winter");
dtSeasons.Rows.Add(2, "Spring");
dtSeasons.Rows.Add(3, "Summer");
dtSeasons.Rows.Add(4, "Autumn");

columnSeasons.DataSource = dtSeasons;
columnSeasons.ValueMember = "Id";
columnSeasons.DisplayMember = "Value";



The code sample is not complete but I hope you get the picture.


这篇关于如何在DataGridViewComboBoxColumn中使用多个数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆