How can I get two winform columns of a listbox in c #?

I'm doing a college student attendance project in the form of a win with MySQL (C #).

That I want to move the data from one list to another list. I have done it. But I load the student name into this. Now the client wants the name and adminno, like datagridview columns.

I am looking for this. Vb has this type of encoding. See List of Multiple Columns . Is this possible in C # ?.

enter image description here

See image below. Its my form ...

enter image description here

My code to download the list

MySqlConnection connection = new MySqlConnection(MyConString); MySqlCommand command = connection.CreateCommand(); MySqlDataReader Reader; command.CommandText = "select name,admin_no from student_admision_master where course='" + course_code + "' AND year='" + year_code + "' AND sem='" + semester_code + "' AND batch='" + batch_code + "'"; connection.Open(); Reader = command.ExecuteReader(); while (Reader.Read()) { listBox1.Items.Add(Reader[0].ToString() + "," + Reader[1].ToString()); } connection.Close(); 

This gives the result of jagadees, 125445. But you want to separate the individual columns.

My code to move data

 private void btn_toAb_Click_Click(object sender, EventArgs e) { int count = listBox1.Items.Count; for (int i = 0; i < count; i++) { listBox2.Items.Add(listBox1.Items[i].ToString()); } listBox1.Items.Clear(); } private void btn_fromAb_Click_Click(object sender, EventArgs e) { int count = listBox2.Items.Count; for (int i = 0; i < count; i++) { listBox1.Items.Add(listBox2.Items[i].ToString()); } listBox2.Items.Clear(); } 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]); //listBox1.add } } 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]); } } 

Thanks in advance!...

+4
source share
2 answers

A Windows Forms ListView control can do this.

+10
source

You can do this using the String.Format () method. Internally, each yoz method determines the alignment of each element in a string. I don't have db, but instead, in my example, I use two arrays (the same as your readers [0] and readers [1]). So, instead of array1 [i] and array2 [i], you use Reader [0] and Reader [1]. Here is an example:

  string[] array1 = { "name 1", "name10", "name104", "name 222", "name 3212" }; string[] array2 = { "12343", "23", "432", "4333", "1" }; const int a = 40; int b = 0; for (int i = 0; i < array1.Length; i++) { if (array1[i].Contains(' ')) b = array1[i].Length - 1; else b = array1[i].Length; b = -(a - b); listBox1.Items.Add(String.Format("{0," + b + "} {1}", array1[i], array2[i])); } 
+2
source

All Articles