当我选择ComboBox时,所有剩余的ComboBox选定项都会被更改 [英] When i select the ComboBox all rest of ComboBox selected items are changed
本文介绍了当我选择ComboBox时,所有剩余的ComboBox选定项都会被更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Iam根据输入动态创建ComboBox。当我选择第一个ComboBox值
其余的所有ComboBox也改变了。
Iam动态添加控件
cmbSource = new ComboBox [dtExelData.Columns.Count];
cmbDestination = new ComboBox [dtExelData.Columns.Count];
for ( int i = 0 ; i < = dtExelData.Columns.Count - 1 ; i ++)
{
cmbSource [i] = new ComboBox();
cmbSource [i] .Name = Convert.ToString( cmbSource + i);
cmbSource [i] .Location = new Point( 100 ,x);
this .Controls.Add(cmbSource [i]);
cmbSource [i] .DataSource = lHeader;
// MessageBox.Show(cmbSource [i] .Name.ToString());
cmbDestination [i] = new ComboBox();
cmbDestination [i] .Name = Convert.ToString( cmbDest + i);
cmbDestination [i] .Location = new Point( 400 ,x);
this .Controls.Add(cmbDestination [i]);
cmbDestination [i] .DataSource = dsTblColumns.Tables [ 0 ];
cmbDestination [i] .DisplayMember = column_name;
cmbDestination [i] .ValueMember = column_name;
x = x + 40 ;
}
请告诉我如何解决这个问题?
解决方案
这是因为你们所有的组合框都使用相同的datasoruce。
每个组合框都必须有数据表。
最佳regrads
为了解决这个问题,我们将Datset复制到新数据集中并将组合框绑定到新数据集
cmbSource = new ComboBox [dtExelData.Columns.Count];
cmbDestination = new ComboBox [dtExelData.Columns.Count];
for(int i = 0; i< = dtExelData.Columns.Count - 1; i ++)
{
cmbSource [i] = new ComboBox();
cmbSource [i] .Name = Convert.ToString(cmbSource+ i);
cmbSource [ i] .Location = new Point(70,x);
PanelControls.Controls.Add(cmbSource [i]);
DataTable newheaderdata = new DataTable(i.ToString());
newheaderdata = dsHeader.Tables [ 0] .Copy();
cmbSource [i] .DataSource = newheaderdata;
cmbSource [i] .DisplayMember = name;
cmbSource [i] .ValueMember =name;
// MessageBox.Show(cmbSource [i] .Name.ToString());
cmbDestination [i] = new ComboBox();
cmbDestination [i] .Name = Convert.ToString(cmbDest+ i);
cmbDestination [i] .Location = new Point(300,x);
PanelControls.Controls.Add(cmbDestination [i]);
DataTable newData = new DataTable(i.ToString());
newData = dsTblColumns.Tables [0]。复制();
cmbDestination [i] .DataSource = newData;
cmbDestination [i] .DisplayMember =name ;
cmbDestination [i] .ValueMember =name;
x = x + 40;
}
Iam Creating The ComboBox Dynamically Based on input . When i select first ComboBox value
rest of The all ComboBox also changed .
Iam Adding Controls Dynamically
cmbSource = new ComboBox[dtExelData.Columns.Count];
cmbDestination = new ComboBox[dtExelData.Columns.Count];
for (int i = 0; i <= dtExelData.Columns.Count - 1; i++)
{
cmbSource[i] = new ComboBox();
cmbSource[i].Name = Convert.ToString("cmbSource" + i);
cmbSource[i].Location = new Point(100, x);
this.Controls.Add(cmbSource[i]);
cmbSource[i].DataSource = lHeader;
// MessageBox.Show(cmbSource[i].Name.ToString());
cmbDestination[i] = new ComboBox();
cmbDestination[i].Name = Convert.ToString("cmbDest" + i);
cmbDestination[i].Location = new Point(400, x);
this.Controls.Add(cmbDestination[i]);
cmbDestination[i].DataSource = dsTblColumns.Tables[0];
cmbDestination[i].DisplayMember = "column_name";
cmbDestination[i].ValueMember = "column_name";
x = x + 40;
}
Please let me know how solve this ?
解决方案
this is because your all comboboxs use same datasoruce .
for every combobox you must have a datasoruce .
best regrads
To Resovle This Problem we have Copy The Datset into New Dataset And Bind The combobox with new dataset
cmbSource = new ComboBox[dtExelData.Columns.Count];
cmbDestination = new ComboBox[dtExelData.Columns.Count];
for (int i = 0; i <= dtExelData.Columns.Count - 1; i++)
{
cmbSource[i] = new ComboBox();
cmbSource[i].Name = Convert.ToString("cmbSource" + i);
cmbSource[i].Location = new Point(70, x);
PanelControls.Controls.Add(cmbSource[i]);
DataTable newheaderdata = new DataTable(i.ToString());
newheaderdata = dsHeader.Tables[0].Copy();
cmbSource[i].DataSource = newheaderdata;
cmbSource[i].DisplayMember = "name";
cmbSource[i].ValueMember = "name";
// MessageBox.Show(cmbSource[i].Name.ToString());
cmbDestination[i] = new ComboBox();
cmbDestination[i].Name = Convert.ToString("cmbDest" + i);
cmbDestination[i].Location = new Point(300, x);
PanelControls.Controls.Add(cmbDestination[i]);
DataTable newData = new DataTable(i.ToString());
newData = dsTblColumns.Tables[0].Copy();
cmbDestination[i].DataSource = newData;
cmbDestination[i].DisplayMember = "name";
cmbDestination[i].ValueMember = "name";
x = x + 40;
}
这篇关于当我选择ComboBox时,所有剩余的ComboBox选定项都会被更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文