How Can i move item collection in Winform Listbox1 to Listbox2?
I am doing Student Attendance project for college in win form with MySQL(C#).
I already ask Hidden Column in Listbox i got the Solution for that.
Updated Query -
string MyConString = ConfigurationManager.ConnectionStrings["College_Management_System.Properties.Settings.cmsConnectionString"].ConnectionString;
MySqlConnection connection = new MySqlConnection(MyConString);
string cnd1 = "select name,admin_no from student_admision_master where course='" + course_code + "' AND year='" + year_code + "' AND sem='" + semester_code + "' AND batch='" + batch_code + "'";
MySqlDataAdapter da = new MySqlDataAdapter(cnd1, connection);
connection.Open();
DataSet ds = new DataSet();
MySqlCommand command = connection.CreateCommand();
da.Fill(ds, "student_admision_master");
//dataGridView1.DataSource = ds.Tables[formName];
listBox1.DataSource = ds.Tables[0].DefaultView;
listBox1.DisplayMember = "name";
listBox1.ValueMember = "admin_no";
connection.Close();
I Got the Student Name in LIstbox. but,.. Listbox to Listbox moving code throw error,..
// All the Listbox2 items r moved to Listbox 3
private void btn_toOd_Click(object sender, EventArgs e)
{
int count = listBox2.Items.Count;
for (int i = 0; i < count; i++)
{
listBox3.Items.Add(listBox2.Items[i].ToString());
}
listBox2.Items.Clear();
}
// All the Listbox3 items r moved to Listbox 2
private void btn_fromOd_Click(object sender, EventArgs e) { int count = listBox3.Items.Count; for (int i = 0; i < count; i++) { listBox2.Items.Add(listBox3.Item开发者_运维技巧s[i].ToString()); } listBox3.Items.Clear(); }
// Selected Items only Move..
private void btn_toAb_Selected_Click(object sender, EventArgs e)
{
int count = listBox1.SelectedItems.Count;
for (int i = 0; i < count; i++)
{
listBox2.Items.Add(listBox1.SelectedItems[i].ToString());
}
for (int i = 0; i < count; i++)
{
listBox1.Items.Remove(listBox1.SelectedItems[0]);
}
}
private void btn_fromAb_Selected_Click(object sender, EventArgs e)
{
int count = listBox2.SelectedItems.Count;
for (int i = 0; i < count; i++)
{
listBox1.Items.Add(listBox2.SelectedItems[i].ToString());
}
for (int i = 0; i < count; i++)
{
listBox2.Items.Remove(listBox2.SelectedItems[0]);
}
}
Items collection cannot be modified when the DataSource property is set. in ARGUMENT EXCEPTION.
How Can i move item collection in Listbox1 to Listbox2,.
And How Can i access the particular ValueMember(admin_no) from the Listboxes.
bzs i want to save the record based on the admin_no.
Is the info is enough or can i add the picture here,..
Please Give an idea to do this!...
Thanks in advance....
Your list box is databound - this means, the items in the list box are automatically managed by the binding manager. You can not really move items around the way you do it.
Instead, you need to modify the underlying data source accordingly. That means: Do not add/delete items to/from the list boxes, but to/from the underlying data set.
Every item in the list box is either a DataRow
or a DataRowView
, which has a Row
property to access the underlying DataRow
. Once you have access to the DataRow
you can determine field values using row['fieldname']
.
To move a row from one dataset to another, you will have to use the ImportRow
method on the destination table to add the row. Then you have to remove the row from the source table. Sample (which you may have to play around with a bit, as I haven't tried it right now):
DataRow row = listBox.SelectedItem;
/* The above may need to be changed to
DataRow row = (listBox.SelectedItem as DataRowView).Row;
in case the SelectedItem is a DataRowView instead of a DataRow
*/
destDataSet.Table.ImportRow(row);
sourceDataSet.Table.Remove(row);
精彩评论