简体   繁体   English

如何在 C# 中获得两列 winform 列表框?

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

I am doing Student Attendance project for college in win form with MySQL(C#).我正在使用 MySQL(C#)以 win 形式为大学做学生出勤项目。

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.现在客户想要名称和 adminno 像 datagridview 列。

I search for this,.我搜索这个,。 Vb has this type of coding. Vb 有这种类型的编码。 See Multi Column Listbox .请参阅多列列表框 Is it Possible in C#?.在 C# 中是否可能?

在此处输入图像描述

See the Below Picture.见下图。 Its my Form.,..它是我的表格.,..

在此处输入图像描述

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.这给出了结果 jagadees,125445。但想要分离不同的列。

My Code For Moving Data's我的移动数据代码

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....提前致谢....

The Windows Forms Control "ListView" can do that. Windows Forms 控件“ListView”可以做到这一点。

You can do it by using String.Format() method.您可以使用 String.Format() 方法来做到这一点。 Inside is each method yoz define the alignment of each item in a row.里面是每个方法 yoz 定义一行中每个项目的 alignment。 I dont have a db, but instead in my example I use two array (same as your reader[0] and reader[1] indexers).我没有数据库,但在我的示例中,我使用了两个数组(与您的 reader[0] 和 reader[1] 索引器相同)。 So Instead of array1[i], and array2[i] you use Reader[0] and Reader[1].因此,您使用 Reader[0] 和 Reader[1] 代替 array1[i] 和 array2[i]。 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]));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#Windows应用程序中将选中的列表框的选定项添加到datagridview中的特定列? - how can i get the selected items of a checked listbox to a particular column in datagridview in c# windows application? 如何在 Winform c# ListView 的两行之间插入行分隔符? - How can I insert a line separator between two rows in a Winform c# ListView? 如何在WinForm C#中获取当前正在运行的应用程序名称和ID? - How can i get Current running Application name and ID in WinForm C#? 如何使用Winform列表框列出C#中声明的对象? - How to use winform listbox to list declared objects in C#? 如何在Winform C#中设置列表框的默认选定项? - How to set default selected item of listbox in winform c#? 如何在C#(Winform)中仅用鼠标制作多选列表框? - How to make multiselect listbox only with mouse in C# (Winform)? 如何在c#中的Winform App中打印图像数组 - How Can i Print Array of Images in Winform App in c# 如何在c#winform中将面板拆分为可点击的细分? - how can I split a panel to clickable segments in c# winform? 我如何 C# 反序列化 JSON 列表 WinForm Combobox? - how can i C# Deserialize JSON list WinForm Combobox? 如何在 C# winform 的文本框中用逗号 (,) 分隔数字? - How can I separate digits with comma (,) in textbox in C# winform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM