使用C#在Windows窗体中将项目从一个CheckedListBox添加到另一个CheckedListBox. [英] Adding Items from one CheckedListBox to another CheckedListBox in Windows Form using C#.

查看:130
本文介绍了使用C#在Windows窗体中将项目从一个CheckedListBox添加到另一个CheckedListBox.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个CheckedListBox 1)chkLstVendor& 2)chkLstVendorAdded和一个添加按钮btn_Add.

I have two CheckedListBox 1) chkLstVendor & 2) chkLstVendorAdded and a Add button btn_Add.

当我检查chkLstVendor中的某些项目并按添加"按钮时,选中的项目将转到chkLstVendorAdded,并且这些项目将从chkLstVendor中删除.

When I check some items in chkLstVendor and press the Add button the checked items will go to chkLstVendorAdded and the items will be removed from chkLstVendor.

我编写的代码如下:

-填充代码 chkLstVendor和 chkLstVendorAdded

公共数据表dtVendor;

public DataTable dtVendor;

公共数据表dtAdd;

public DataTable dtAdd;

DataView dv1;

DataView dv1;

DataView dv2;

DataView dv2;

公共无效frm_Load(.......)

public void frm_Load(.......)

{

CreateTable();

CreateTable();

PopulateVendor();

PopulateVendor();

PopulateVendorAdd();

PopulateVendorAdd();

}

私有无效PopulateVendor()

private void PopulateVendor()

{

dtVendor = DBCon.Result(".....");     //DBCon是连接dll.

dtVendor=DBCon.Result(".....");     // DBCon is connection dll.

dv1 =新的DataView(dtVendor);

dv1=new DataView(dtVendor);

chkLstVendor .DataSource = dv1;

chkLstVendor .DataSource=dv1;

chkLstVendor.DisplayMember ="VendorName";

chkLstVendor.DisplayMember="VendorName";

chkLstVendor.ValueMember ="VendorID";

chkLstVendor.ValueMember="VendorID";

}

私有无效CreateTable()

private void CreateTable()

{

//在这里,我向dtAdd类型的Int(VendorID)和String(VendorName)添加了两行

//Here I am adding two rows to the dtAdd of type Int(VendorID) and String(VendorName)

}

private void&PopulateVendorAdd()

private void PopulateVendorAdd()

{

dv2 =新的DataView(dtAdd);

dv2=new DataView(dtAdd);

chkLstVendorAdd.DataSource = dv2;

chkLstVendorAdd.DataSource=dv2;

chkLstVendorAdd.DisplayMember ="VendorName";

chkLstVendorAdd.DisplayMember="VendorName";

chkLstVendorAdd.ValueMember ="VendorID";

chkLstVendorAdd.ValueMember="VendorID";

}

private void btn_Add_Click(对象发送者,EventArgs e)

private void btn_Add_Click(object sender, EventArgs e)

{

DataRow rows = new DataRow();

DataRow rows=new DataRow();

dtAdd.NewRow();

dtAdd.NewRow();

for(int i = 0; i< chkLstVendor.CheckedItems.Count; i ++)

for(int i = 0; i < chkLstVendor.CheckedItems.Count; i++)

{

DataRow row =(DataRow)((DataRowView)chkLstVendor.CheckedItems [i]).Rows;

DataRow row=(DataRow)((DataRowView)chkLstVendor.CheckedItems[i]).Rows;

rows [0] = row ["VendorID"];

rows[0]=row["VendorID"];

rows [1] = row ["VendorName"];

rows[1]=row["VendorName"];

dtAdd.AddRows(rows);

dtAdd.AddRows(rows);

}

//将chkLstVendor的已检查项目添加到chkLstVendorAdd的这一部分工作正常.

// This part of adding the checked items from chkLstVendor to chkLstVendorAdd is working fine.

//但现在我不得不从chkLstVendor中删除选中的项目.我写的代码只是删除

//But now I am stuck in removing the checked items from chkLstVendor. The code that I have written is only removing

//一项.

for(int i = 0; i< chkLstVendor.CheckedItems.Count; i ++)

for(int i = 0; i < chkLstVendor.CheckedItems.Count; i++)

{

DataRow row =(DataRow)((DataRowView)chkLstVendor.CheckedItems [i]).Rows;

DataRow row=(DataRow)((DataRowView)chkLstVendor.CheckedItems[i]).Rows;

int id = Convert.toInt32(row ["VendorID"].toString());

int id=Convert.toInt32(row["VendorID"].toString());

DataRow [] rows = dtVendor.Select("VendorID =" + a);

DataRow[] rows=dtVendor.Select("VendorID=" +a);

foreach(行中的DataRow rw)

foreach(DataRow rw in rows)

{

rw.Delete();

rw.Delete();

}

}

chkLstVendor.Refresh();

chkLstVendor.Refresh();

chkLstVendorAdd.Refresh();

chkLstVendorAdd.Refresh();

//现在此代码仅删除一个条目,即假设我已经检查了两个项目并按下了添加按钮

// Now this code is only deleting one entry, i.e. supposed I have checked two items and pressed the Add button

//chkLstVendor填充了两个选中的项,但是仅删除了一个选中的项

// the chkLstVendor is populated with both the checked items, but only one checked item is deleted

//  chkLstVendor

// from chkLstVendor

//我尝试使用DataView.RowFilter属性,但无法正确执行.

}

请帮助.我处于一个非常绝望的情况.预先感谢.

Pradeep(PKM)

Pradeep(PKM)

推荐答案

尝试如下更改for语句:

Try changing the for statement as follows:

for(int i = 1; i< = chkLstVendor.CheckedItems.Count; i ++)

for(int i = 1; i <= chkLstVendor.CheckedItems.Count; i++)

看看是否有帮助.


这篇关于使用C#在Windows窗体中将项目从一个CheckedListBox添加到另一个CheckedListBox.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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