簡體   English   中英

如何在 C# 中獲得兩列 winform 列表框?

[英]How Can i get Two column winform listbox in C#?

我正在使用 MySQL(C#)以 win 形式為大學做學生出勤項目。

在那我想將數據從一個列表框移動到另一個列表框。 我做到了。 但是我在其中加載了學生姓名。 現在客戶想要名稱和 adminno 像 datagridview 列。

我搜索這個,。 Vb 有這種類型的編碼。 請參閱多列列表框 在 C# 中是否可能?

在此處輸入圖像描述

見下圖。 它是我的表格.,..

在此處輸入圖像描述

我的加載列表框的代碼

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

提前致謝....

Windows Forms 控件“ListView”可以做到這一點。

您可以使用 String.Format() 方法來做到這一點。 里面是每個方法 yoz 定義一行中每個項目的 alignment。 我沒有數據庫,但在我的示例中,我使用了兩個數組(與您的 reader[0] 和 reader[1] 索引器相同)。 因此,您使用 Reader[0] 和 Reader[1] 代替 array1[i] 和 array2[i]。 這是示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM