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

标签 c# winforms

我正在使用 MySQL(C#) 以 win 形式为大学做学生出勤项目。

因为我想将数据从一个列表框移动到另一个列表框。我做到了。但是我在其中加载了学生姓名。现在客户想要名称和 adminno 像 datagridview 列。

我搜索这个,。 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]);
    }
}

提前致谢!...

最佳答案

Windows 窗体控件“ListView”可以做到这一点。

关于c# - 我如何在 C# 中获得两列 winform 列表框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6775284/

相关文章:

c# - 关闭表单时仍在运行的后台工作程序会发生什么情况?

c# - 命名约定 : Guidelines for verbs/nouns and english grammar usage

c# - USQL 查询从 Json 数据创建表

c# - 通过服务器从 MySQL 数据库检索图像

c# - 使用 NativeWindow 禁用屏幕保护程序

c# - 标签在 OnDraw 事件中动态创建并可从 Visual Studio Designer 编辑

c# - UWP:后台任务中的音频媒体捕获

c# - 如何将 ILogger 注入(inject) EFCore DbContext

c# - 调用 VBA 函数包含 ActiveWorkbook.Close 会导致 Excel 崩溃

c# winforms - 在模式窗体之间传递参数