2011-07-21 78 views
4

我正在用MySQL(C#)以win形式進行大學學生考勤項目。如何在C#中獲得兩列winform列表框?

因爲我想將數據一個列表框移動到另一個列表框。我做到了。但我加載學生的名字。現在,客戶想要的名稱和adminno就像datagridview Columns一樣。

我搜索這個,。 Vb有這種類型的編碼。請參閱Multi Column Listbox。在C#中可能嗎?

enter image description here

請參閱下圖。它的我的形式。..

enter image description here

我的代碼加載列表框

 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(); 

這給結果jagadees,125445. 但想不同的單獨的列。

我的代碼移動數據的

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]); 
    } 
} 

提前感謝!...

回答

10

的Windows窗體控件 「ListView的」 能做到這一點。

+0

感謝信息,, – Sagotharan

2

你可以通過使用String.Format()方法來實現。裏面是每個方法yoz定義行中每個項目的對齊方式。 我沒有數據庫,但在我的例子中,我使用了兩個數組(與您的reader [0]和reader [1]索引器相同)。因此,您可以使用Reader [0]和Reader [1]而不是array1 [i],array2 [i]。 這裏是exampe:

 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])); 
     }