从Listbox1如何使用csharp将特定项添加到Listbox2 [英] From Listbox1 how to add Particular item to Listbox2 using csharp

查看:94
本文介绍了从Listbox1如何使用csharp将特定项添加到Listbox2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows应用程序中的c sharp中的列表框进行应用。



设计如下;



Listbox1按钮Listbox2



Ram ADD

Sam添加所有

Suresh删除

Vignesh删除全部

加载

清除



SqlConnection con = new SqlConnection(Data Source = INDIA; Initial Catalog = HIMTTESTing; Integrated Security = True);



代码如下按钮(Load)

i am doing application using List box in c sharp in windows application.

Design as follows;

Listbox1 Buttons Listbox2

Ram ADD
Sam ADD ALL
Suresh Remove
Vignesh Remove All
Load
Clear

SqlConnection con = new SqlConnection(" Data Source=INDIA;Initial Catalog=HIMTTESTing;Integrated Security=True");

Code as follows for button(Load)

private void Btn_Load_Click(object sender, EventArgs e)
{
  string sql = "select Faculty_Name from  Tb_SCH_Faculty_List";
  DataSet mydataset = new DataSet();
  SqlDataAdapter adp = new SqlDataAdapter(sql, con);
  adp.Fill(mydataset,"Tb_SCH_Faculty_List");
  DataTable mydatatable  = mydataset.Tables[0];
  DataRow temprow = null;

  foreach (DataRow tempRow_Variable in mydatatable.Rows)
  {
   temprow  = tempRow_Variable;
   Lb_Faculty_Name.Items.Add((temprow["Faculty_Name"]));
  }
}





代码如下按钮(全部添加)



Code as follows for button(ADD ALL)

private void Btn_Add_All_Click(object sender, EventArgs e)
{
  string com = " Select Faculty_Name from Tb_SCH_Faculty_List";
  DataSet mydataset = new DataSet();
  SqlDataAdapter adpt = new SqlDataAdapter(com, con);
  adpt.Fill(mydataset, "Tb_SCH_Faculty_List");
  DataTable mydatatable = mydataset.Tables[0];
  DataRow temprow = null;

  foreach (DataRow tempRow_Variable in mydatatable.Rows)
  {
    temprow = tempRow_Variable;
    Lb_Selected_Faculty.Items.Add((temprow["Faculty_Name"]));
  }
}





加载和ADD全部按钮都正常工作。



输出显示为;

当我点击加载(按钮)时,从数据库中检索所有教员名称并显示在Lisbox1中。



同样当我点击ADD ALL(按钮)时,所有教员姓名都显示在Lisbox2中。



以上两个按钮加载和ADD ALL工作正常。

在ADD按钮中;



当我点击加载按钮时,所有名称都从数据库中检索出来当我选择一个特定名称并单击特定名称要显示在列表框2中的添加按钮时,在Listbox1中显示在Listbox1中。



为特定名称显示怎么办。请帮助我。



/编辑由Jibesh添加的预标签

推荐答案

处理表示层的方式看起来不太好。由于您的UI应该将从第一个列表框中选择的项目移动到第二个列表框,除非您正在进行任何其他检查,否则您不需要进行数据库操作。



只需从数据库填充ListBox1并使用ListBox属性进行进一步操作。我相信正常的步骤是



1.从数据库中填写ListBox1

2.在列表框中选择顶部项目作为选中使用lstBox1.SelectedIndex = 0;

3.从listBox1中选择项目

4.OnPressing AddAll按钮使用lstBox1.SelectedItems属性迭代所选项目并使用listBox2添加到listbox2 .Items.Add(..)方法

5.从listBox1中删除已移动的项目





如果你想了解更多,请告诉我。
The way you handled the presentation layer doesnt look good. since your UI should move the items selected from the first listbox to the second list box, you dont need to go for database operation unless you are doing any other checking.

Just fill the ListBox1 from database and use the ListBox properties for further operations. The normal steps followed to my belief are

1.Fill the ListBox1 from database
2.Select top item in the list box as selected using lstBox1.SelectedIndex = 0;
3.Select the item(s) from the listBox1
4.OnPressing the AddAll button iterate the selected Items using the lstBox1.SelectedItems property and add to the listbox2 using listBox2.Items.Add(..) method
5.Remove the moved item from the listBox1


let me know if you want know more.


这篇关于从Listbox1如何使用csharp将特定项添加到Listbox2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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