multicolumn combo box in asp.net - c#

I have a Database table with two fields MainAccount and MainDescription table have following data MainAccount 10 MainDescription Capital,MainAccount 20 MainDescription Account Receiable,MainAccount 30 MainDescription Account Payable
I have a asp.net page with DropDownList and a text box. I want to get all data in DropDownList here is my code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlDataAdapter adapter = new SqlDataAdapter("select distinct MainAccount,MainDescription from chart", db.con))
{
DataTable datatable = new DataTable();
adapter.Fill(datatable);
List<ListItem> list = new List<ListItem>();
foreach (DataRow dr in datatable.Rows)
{
string descr = string.Format("{0} | {1}", dr[0].ToString(), dr[1].ToString());
list.Add(new ListItem(descr));
}
list = list.OrderBy(a => a.Text).ToList();
DropDownList1.DataSource = list;
DropDownList1.DataBind();
}
}
}
this code is working fine to display all MainAccount and Main Description in DropDownList.
This is what I have now:
And this is the result I'm looking for:

To get the second picture result , I have tried this with example , see the two images below.
Add a property class
public class Columns
{
// obviously you find meaningful names of the 2 properties
public string MainAccount { get; set; }
public string MainDescription { get; set; }
public string Concatenate {get ; set ;}
}
then
if (!IsPostBack)
{
using (SqlDataAdapter adapter = new SqlDataAdapter("select distinct MainAccount,MainDescription from chart", db.con))
{
DataTable datatable = new DataTable();
adapter.Fill(datatable);
List<Columns> list = new List<Columns>();
foreach (DataRow dr in datatable.Rows)
{
list.Add(new Columns { MainAccount =dr[0].ToString() ,
MAinDescription = dr[1].ToString(),
Concatenate = string.Format("{0} | {1}",dr[0].ToString(), dr[1].ToString())});
}
list = list.OrderBy(a => a.MainAccount).ToList();
DropDownList1.DataSource = list;
DropDownList1.DataValueField = "MainDescription";
DropDownList1.DataTextField = "Concatenate";
DropDownList1.DataBind();
}
}
Then use the on SelectedIndexChanged property of the dropdown and set your text box value.
Example i just run this below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DropDown
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Columns> list = new List<Columns>();
for (int i = 0; i < 10; i++)
{
list.Add(new Columns
{
MainAccount = "1" + i,
MainDescription = "Main Account Desc" + i,
Concatenate = string.Format("{0} | {1}", "1" + i, "Main Account Desc" + i)
});
}
DropDownList1.DataSource = list;
DropDownList1.DataValueField = "MainDescription";
DropDownList1.DataTextField = "Concatenate";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedValue.ToString();
}
}
}
Below is the resulting image.
Second image
Also i noticed you changed the combobox text to the mainAccount after selection. Just to let you know you cant just Reset the combobox text to an item that does not exist in the list . However i suggest you use a public array or struct, populate your array onload, then later populate your list from the array. After selection, you can then refresh your dropdown from the array/list as well and reset the datatextfield to your mainAccount column. Then set the selected to a row where the MainDescription column = Textbox text. I hope this helps. Let us know if you need more help achieving this ?

You can bind MainAccount (i.e. 10,20,30,40) to dropdownlist.
And on selected index change you can display MainDescription.
like this-
DataRow[] drDesc= dt.Select(filter expression)

i think that the best solution may be by creating a class that contains a definition of two variables and set the datasource of the DropDownlist by a list of this class
public class DropDownDataSource
{
public string Text { set; get; }
public string Value { set; get; }
}
then we will modify your code to work with the new way
List<DropDownDataSource> list = new List<DropDownDataSource>();
foreach (DataRow dr in datatable.Rows)
{
var item = new DropDownDataSource
{
Text = dr[0].ToString() + " | " + dr[1].ToString(),
Value = dr[1].ToString()
};
list.Add(item);
}
DropDownList1.DataSource = list;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
and in the SelectedIndexchanged Event of the DropDownList1 we will write this code to bring the value of selected item and bint it in the textbox
protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
{
TextBoxValue.Text = DropDownList1.SelectedValue;
}

Related

How to add control cells programmatically in a gridview C# .net?

Every row have to has a dropdownlist and a submit button.
So I made a List for data.
I added them like this.
enter image description here
//In my code behind
List<data> listdatainfo = new List<data>();
protected void Button1_Click(object sender, EventArgs e){
SqlDataReader detaillist = comm2.ExecuteReader();
while (detaillist.Read())
{
rmainfo tempinfo = new rmainfo();
tempinfo.itemdetail= detaillist["itemdetail"].ToString();
tempinfo.creditmemo= detaillist["creditmemo"].ToString();
tempinfo.submit= "0";//it will be filled 0 or 1
listdatainfo .Add(tempinfo);
}
loadDataTable();}
//it was referenced from here http://asp.net-informations.com/gridview/without-database.htm
private void loadDataTable()
{
DataSet ds = new DataSet();
DataTable dt;
dt = new DataTable();
DataColumn itemdetail;
DataColumn creditmemo ;
CommandField submit = new CommandField();
submit.EditText = "Edit";
submit.ShowEditButton = true;
itemdetail= new DataColumn("itemdetail",Type.GetType("System.String"));
creditmemo = new DataColumn("creditmemo ",Type.GetType("System.String"));
submit = new CommandField();
dt.Columns.Add(itemDetail);
dt.Columns.Add(creditMemo);
dt.Columns.Add("submit"); //it's for submit button
foreach (data tempinfo in listdatainfo )
{
DataRow dr;
dr = dt.NewRow();
dr["Item Detail"] = tempinfo.itemDetail;
dr["Credit Memo"] = tempinfo.creditMemo;
dr["submit"] = submit;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
}}
public class data
{
public string itemDetail { get; set; }
public string creditMemo { get; set; }
public string submit { get; set; }
}
As i expected, this line occured an error this line.
dr["submit"] = submit;
How can i add a button each row? or any component?
It was easier in classic asp.....
Please help.
You have to add the buttons in the GridView model instead of the datatable that is feeding it.

How to filter datagridview based on combobox selection

I have a DatagridView contains students information, including class, age, etc. now I want to create a ComboBox that have function to filter which class (for example if I select class IIA on combobox, the datagridview only show students from class IIA).
Here is my code in the form to load the data from objectbindingsource:
private void frmdbSiswa_Load(object sender, EventArgs e)
{
db = new SiswaSMSEntities();
tabelSiswaBindingSource.DataSource = db.Tabel_Siswa.ToList();
agamaSiswaBindingSource.DataSource = db.Agama_Siswa.ToList();
kelasBindingSource.DataSource = db.Kelas.ToList();
jenisKelaminBindingSource.DataSource = db.Jenis_Kelamin.ToList();
dataGridViewSiswa.DataSource = db.Tabel_Siswa.ToList();//to show the datagridview
cboKelas.DataSource = db.Kelas.ToList();//combobox
}
and here is the code for combobox:
private void cboKelas_SelectionChangeCommitted(object sender, EventArgs e)
{
dataGridViewSiswa.DataSource = db.Tabel_Siswa.Where(x => x.IdKelas == cboKelas.SelectedIndex).ToList();
}
I also bind the combobox to the datasource.
I am so confuse for hours working on this. I am new to programming, so please forgive me if I am asking a very basic question.
The problem is, when I run the code, it does filter the data, but when I select the class IA, datagridview shows nothing, and when I select class IB, the datagridview shows students from class IA and so on. and also when I select the datagridview, combobox on show system.Data.Entity.Error.
Upon combo box selection changed event, set data grid view RowFilter property to selected value :
dv.RowFilter = "YourColumnName = ComboBoxSelectedValue";
Updating this with similar problem/solution here
Repalce this Add !IsPostBack in Page Load
private void frmdbSiswa_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
db = new SiswaSMSEntities();
tabelSiswaBindingSource.DataSource = db.Tabel_Siswa.ToList();
agamaSiswaBindingSource.DataSource = db.Agama_Siswa.ToList();
kelasBindingSource.DataSource = db.Kelas.ToList();
jenisKelaminBindingSource.DataSource = db.Jenis_Kelamin.ToList();
dataGridViewSiswa.DataSource = db.Tabel_Siswa.ToList();//to show the datagridview
dataGridViewSiswa.DataBind();
cboKelas.DataSource = db.Kelas.ToList();//combobox
cboKelas.DataBind();
}
}
private void cboKelas_SelectionChangeCommitted(object sender, EventArgs e)
{
dataGridViewSiswa.DataSource = db.Tabel_Siswa.Where(x => x.IdKelas == cboKelas.SelectedIndex).ToList();
dataGridViewSiswa.DataBind();
}
Can you try it like this?
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Product Name";
dataGridView1.Columns[2].Name = "Product Price";
string[] row = new string[] { "1", "Product 1", "1000" };
dataGridView1.Rows.Add(row);
row = new string[] { "2", "Product 2", "2000" };
dataGridView1.Rows.Add(row);
row = new string[] { "3", "Product 3", "3000" };
dataGridView1.Rows.Add(row);
row = new string[] { "4", "Product 4", "4000" };
dataGridView1.Rows.Add(row);
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Data";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 4;
cmb.Items.Add("True");
cmb.Items.Add("False");
dataGridView1.Columns.Add(cmb);
}
}
}
Post back if you have additional questions.
http://csharp.net-informations.com/datagridview/csharp-datagridview-combobox.htm
Unless you absolutely have to have a List<T> as a DataSource to the grid, I suggest you use a DataTable as a DataSource to the grid. It may make things easier and it appears you may already have the DataTable from db.Tabel_Siswa.ToList() which appears to be making a list FROM a table.
With that said, below is an example of using a combo box to filter the data in a grid using a DataTable and a DataView. It is a simple form with an empty DataGridView and an empty ComboBox. A global DataTable variable gridTable is used as a DataSource to the grid. A global DataView variable dv is used to “filter/unFilter” the gridTable.
Upon form load, columns and some test data is added to the gridTable. Then the gridTable is set as a DataSource to the grid. Lastly the ComboBox is filled with items “Class A, Class B” etc… A “No Filter” selection is added to the combo box to allow the user to remove an applied filter.
When the user changes the combo box selection, a new DataView is created and it’s RowFilter is set to the selected value in the combo box. Finally, the DataView dv is set as a DataSource to the grid to display the filtered results
DataTable gridTable;
DataView dv;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
gridTable = GetTable();
FillTable(gridTable);
dataGridView1.DataSource = gridTable;
SetComboBox();
}
private void SetComboBox() {
comboBox1.Items.Add("No Filter");
comboBox1.Items.Add("Class A");
comboBox1.Items.Add("Class B");
comboBox1.Items.Add("Class C");
comboBox1.SelectedIndex = 0;
}
private DataTable GetTable() {
DataTable dt = new DataTable();
dt.Columns.Add("FName", typeof(string));
dt.Columns.Add("LName", typeof(string));
dt.Columns.Add("Class", typeof(string));
return dt;
}
private void FillTable(DataTable dt) {
for (int i = 1; i < 5; i++) {
dt.Rows.Add("FName" + i, "LName" + i, "Class A");
dt.Rows.Add("FName" + i, "LName" + i, "Class B");
dt.Rows.Add("Class" + i, "Class" + i, "Class C");
}
}
The RowFilter string… dv.RowFilter = "Class = '" + comboBox1.Text + "'" says to filter such that the cells in the column named “Class” equals the text in the combo box. Example: “Class = Class B”. This implies that the DataSource has a column name that “matches” what’s in the filter string.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
dv = new DataView(gridTable);
if (comboBox1.Text == "No Filter")
dv.RowFilter = "";
else
dv.RowFilter = "Class = '" + comboBox1.Text + "'";
dataGridView1.DataSource = dv;
}
Hope this helps.

Adding multiple row from one datagridview to datagridview c# windows form

i have this piece of code through which i am inserting one grid view data to another
private void btnADD_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("LOB");
dt.Columns.Add("Quantity");
dt.Columns.Add("Name");
dt.Columns.Add("Packing");
dt.Columns.Add("Price");
dt.Columns.Add("Code");
dr = dt.NewRow();
dr["LOB"] = txtLOB.Text;
dr["Quantity"] = txtQuantity.Text;
dr["Name"] = txtName.Text;
dr["Packing"] = txtPacking.Text;
dr["Price"] = txtPrice.Text;
dr["Code"] = txtBachNo.Text;
dt.Rows.Add(dr);
gridviewDtaInserted.DataSource = dt;
}
i am able to insert one row at a time but i want to insert many rows one after another.
You should declare DataTable as globally because every time on Button click it has instantiated with new key word.
Try this:
DataTable dt = new DataTable();
dt.Columns.Add("LOB");
dt.Columns.Add("Quantity");
dt.Columns.Add("Name");
dt.Columns.Add("Packing");
dt.Columns.Add("Price");
dt.Columns.Add("Code");
private void btnADD_Click(object sender, EventArgs e)
{
DataRow dr;
for(int i = 0; i <= RowsCountThatYouWantToIsert; i++)
{
dr = dt.NewRow();
dr["LOB"] = txtLOB.Text;
dr["Quantity"] = txtQuantity.Text;
dr["Name"] = txtName.Text;
dr["Packing"] = txtPacking.Text;
dr["Price"] = txtPrice.Text;
dr["Code"] = txtBachNo.Text;
dt.Rows.Add(dr);
}
gridviewDtaInserted.DataSource = dt;
}
IF you are reading from one view to another you can use a looping structure to go through each iteration. I would suggest a for loop so that you can use the current numerical iteration as part of the instruction. if you want to ammend the first view to the second then you may want to use
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
dt=(DataTable)DataGridViewer1.datasource;
dt2=(DataTable)DataGridViewer2.datasource;
dt2.Merge(dt);
DataGridViewer2.datasource=dt2;
The DataSource property, from a DataGridView accepts a collection of objects.
So, I would suggest you to add how many rows you want to that public collection of objects, and at the end to update the DataSource
gridviewDtaInserted.DataSource = myCollection;
See here more info: MSDN
Also, here is a nice question may help you.
As a skeleton you can design in this way:
public class TestFunctional {
public TestFunctional(){
DataItems = new List<DataItem>();
}
public List<DataItem> DataItems { get; set; }
private void AddOneItem(){
var newItem = new DataItem {
LOB = "a",
Quantity = 1,
Name = "A",
Packing = true,
Code = "a1"
};
DataItems.Add(newItem);
RefreshGrid();
}
private void AddMultipleItems(){
var newItem1 = new DataItem {
LOB = "a",
Quantity = 1,
Name = "A",
Packing = true,
Code = "a1"
};
var newItem2 = new DataItem {
LOB = "b",
Quantity = 2,
Name = "B",
Packing = false,
Code = "b2"
};
DataItems.Add(newItem1);
DataItems.Add(newItem2);
/*or use DataItems.AddRange( ... ) */
RefreshGrid();
}
private void RefreshGrid()
{
gridviewDtaInserted.Rows.Clear();
gridviewDtaInserted.Refresh();
gridviewDtaInserted.DataSource = DataItems;
}
}
public class DataItem{
public string LOB { get; set; }
public double Quantity { get; set; }
public string Name { get; set; }
public bool Packing { get; set; }
public decimal Price { get; set; }
public string Code { get; set; }
}
I hope it will help you. Otherwise, ask :)
Edit:
Also, try to use the BindingList instead of List, I am not sure, but maybe it will automatically update the DataSource of the grid as soon an item is inserted in the collection.
BindingList<DataItem> DataItems

not able to set default value in combobox : C#

I was trying to add a default value ("--select item--") in existing ComboBox which is getting filled by Database table. Here is my code.
SqlConnection conn = new SqlConnection("Server = .\\SQLEXPRESS; Initial Catalog= Student; Trusted_Connection = True");
string query = "select Id, Name from abc1";
SqlDataAdapter da = new SqlDataAdapter();
conn.Open();
DataTable dt = new DataTable();
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader reader = command.ExecuteReader();
dt.Load(reader);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
Everything is working fine in above code and I'm getting CB filled with desired values.
Now when I try to insert default value using below, then it is throwing an error. This way was tried here
comboBox1.Items.Insert(0, "Select"); //this is throwing an error
comboBox1.SelectedIndex = 0;
I further explored below code to add default item.
comboBox1.SelectedIndex = -1;
comboBox1.Text = "Select an item";
This added an item as desired, but on any event, CB loses this value. After SelectIndexChanged event, I lose this value. So this cannot be my solution.
Any advise?
I'd rather not intefere into binding, but edit SQL instead:
string query =
#"select 0 as Id, -- or whatever default value you want
'select' as Name,
0,
union all
-- your original query here
select Id,
Name,
1
from abc1
-- to have default value on the top
order by 3 asc";
Insert's second parameter is expecting a ComboBox.ObjectCollection object.
Try doing this:
Having a class
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
}
And using:
// This could be inline, but for simplicity step by step...
ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;
comboBox1.Items.Add(item);
//or
comboBox1.Items.Insert(0, item);
You can add programmatically the item to the datatable
DataRow dr = dt.NewRow();
dr["id"] = "0";
dr["name"] = "Select";
dt.Rows.InsertAt(dr, 0);
then you can set the datasource
comboBox1.DataSource = dt;
If you just want to show a suggestion text to your user, the "EDIT" part of this answer may help (can only work if your ComboBox's DropDownStyle is not set to DropDownList).
But if you really want some "Select" item in your ComboBox, try this:
void Form1_Load(object sender, EventArgs e)
{
//load your datatable here
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Name";
comboBox1.Items.Add(new { ID = 0, Name = "Select" });
foreach (DataRow a in dt.Rows)
comboBox1.Items.Add(new { ID = a["ID"], Name = a["Name"] });
comboBox1.SelectedIndex = 0;
comboBox1.DropDown += new EventHandler(comboBox1_DropDown);
comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
}
void comboBox1_DropDownClosed(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains(new { ID = 0, Name = "Select" }))
{
comboBox1.Items.Insert(0, new { ID = 0, Name = "Select" });
comboBox1.SelectedIndex = 0;
}
}
void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains(new { ID = 0, Name = "Select" }))
comboBox1.Items.Remove(new { ID = 0, Name = "Select" });
}

listbox error: Items collection cannot be modified when the DataSource property is set

I have 2 listboxes in a window form, one on left and one on right. The
1st listbox have some items while the 2nd listbox is empty. Also there
are 2 buttons between the 2 listboxes which used to move item from/to
the 1st and 2nd listbox
My problem here is that after I bind the data to the 1st listbox (from
a database, using DisplayMember and ValueMember) , and I try to move 1
of the item from this 1st listbox to the 2nd listbox and I want that
the selected item is also removed from the 1st listbox by:
private void btnMoveRight_Click(object sender, EventArgs e)
{
ADD();
}
private void ADD()
{
int c = listJobBox.Items.Count - 1;
` for(int i= c; i>=0; i--)
{
if(listJobBox.GetSelected(i))
{
lstAssignedJobs.Items.Add(listJobBox.Items[i]);
listJobBox.Items.Remove(listJobBox.SelectedItem); ---error line
But the selected item is not removed from the 1st listbox.
it displays error message "Items collection cannot be modified
when the DataSource property is set."
can any one give me the solution to my problem.
Add a boolean column to your DataTable object, something like IsSelected.
Then instead of binding your listbox1 directly to the table, bind it to a BindingSource. Add 2 bindingsources to your form using the designer. And place this code in your code behind file.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.InitializeDataObjects();
}
private void InitializeDataObjects()
{
this.InitData();
this.InitBindingSources();
}
private void InitData()
{
ds = new DataSet();
var dt = new DataTable("Table1");
dt.Columns.Add("Name", typeof(string));
ds.Tables.Add(dt);
}
private void InitBindingSources()
{
bindingSource1 = new BindingSource();
bindingSource2 = new BindingSource();
bindingSource1.DataSource = ds;
bindingSource1.DataMember = "Table1";
bindingSource2.DataSource = ds;
bindingSource2.DataMember = "Table1";
listBox1.DataSource = bindingSource1;
listBox1.DisplayMember = "Name";
listBox2.DataSource = bindingSource2;
listBox2.DisplayMember = "Name";
}
}
Then when you load your data, do the following:
private void btnLoadAndBind_Click(object sender, EventArgs e)
{
this.FetchData(this.ds.Tables["Table1"]);
this.AddSelectedColumn(this.ds.Tables["Table1"]);
this.bindingSource1.Filter = "IsSelected = false";
this.bindingSource2.Filter = "IsSelected = true";
}
private void FetchData(DataTable dataTable)
{
string CS = "your connectionstring";
using (SqlConnection con = new SqlConnection(CS))
{
try
{
SqlDataAdapter da = new SqlDataAdapter();
con.Open();
var sqlcmd = new SqlCommand("SELECT Name FROM sometable", con);
sqlcmd.CommandType = CommandType.Text;
da.SelectCommand = sqlcmd;
da.Fill(dataTable);
}
catch (Exception ex)
{
MessageBox.Show("exception raised");
throw ex;
}
}
}
private void AddSelectedColumn(DataTable suppliersDataTable)
{
var dc = new DataColumn("IsSelected", typeof(bool));
suppliersDataTable.Columns.Add(dc);
foreach (DataRow dr in suppliersDataTable.Rows)
{
dr["IsSelected"] = false;
}
}
Now your listboxes are both connected to the same datatable and filtered based on the IsSelected property / column. Just set this column to true or false and it will flip from box to box. Your eventhandler of a button could look like this:
public void button_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Current!= null)
{
var dr = ((DataRowView)this.bindingSource1.Current).Row;
dr["IsSelected"] = true;
}
}
This works!
Things will be become much simpeler if you use a typed dataset. Most of the bindings then can be done in the designer and your code behind will shrink to 20 lines of code....
Lets say listbox1 is bound to datatable1 (it could be any other collection type) and listbox2 is bound to datatable2. When you click on move button, remove the selected item from the collection i.e datatable1 and add that item to other collection i.e. datatable2 and re-bind the listbox1 and lisbox2.
Here is a rough working example:
public partial class Form1 : Form
{
private DataTable _dataSource1;
private DataTable _dataSource2;
public Form1()
{
InitializeComponent();
_dataSource1 = GetData1();
_dataSource2 = GetData2();
Initialize();
}
private void btnMove_Click(object sender, EventArgs e)
{
MoveItem();
}
void Initialize()
{
listBox1.DataSource = _dataSource1;
listBox1.DisplayMember = "Fruits";
listBox1.ValueMember = "Fruits";
listBox2.DataSource = _dataSource2;
listBox2.DisplayMember = "Fruits";
listBox2.ValueMember = "Fruits";
}
DataTable GetData1()
{
var dt = new DataTable();
dt.Columns.Add("Fruits");
dt.Rows.Add(new object[] {"Apple"});
dt.Rows.Add(new object[] { "Orange" });
dt.Rows.Add(new object[] { "Grapes" });
return dt;
}
DataTable GetData2()
{
var dt = new DataTable();
dt.Columns.Add("Fruits");
return dt;
}
void MoveItem()
{
var index = listBox1.SelectedIndex;
var dataRowToRemove = _dataSource1.Rows[index];
var listItem = dataRowToRemove[0] as string;
_dataSource1.Rows.Remove(dataRowToRemove);
var dataRowToAdd = _dataSource2.NewRow();
dataRowToAdd[0] = listItem;
_dataSource2.Rows.Add(dataRowToAdd);
Initialize();
}
}

Categories

Resources