开发者

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

I am doing Student Attendance project for college in win form with MySQL(C#).

In that I want to move data one listbox to another listbox. I done that. But i load the student name in that. Now Client Want Name and adminno like datagridview Columns.

I search for this,. Vb has this type of coding. See Multi Column Listbox. Is it Possible in C#?.

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

See the Below Picture. Its my Form.,..

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

My Code For Loading Listbox

        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 give the result jagadees, 125445. But want to disparate Separate Columns.

My Code For Moving Data's

private void btn_toAb_Click_Click(object sender, EventA开发者_如何学编程rgs 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!...


The Windows Forms Control "ListView" can do that.


You can do it by using String.Format() method. Inside is each method yoz define the alignment of each item in a row. I dont have a db, but instead in my example I use two array (same as your reader[0] and reader[1] indexers). So Instead of array1[i], and array2[i] you use Reader[0] and Reader[1]. Here is the 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]));
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜