gpt4 book ai didi

c# - 在 ListView 中拉取和拆分数据库项目

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:58 24 4
gpt4 key购买 nike

我已经编写了一些代码来简单地从紧凑型 SQL 服务器 (4.0) 中提取数据库信息。目前我只是用几个空格将检索到的每个项目分开,但我想知道如何拉出每个项目并使其与标题相对应。在这里初始化:

初始化 ListView

private void InitializeListView()
{
// Set the view to show details.
lbxBugged.View = View.Details;

// Allow the user to rearrange columns.
lbxBugged.AllowColumnReorder = true;

// Select the item and subitems when selection is made.
lbxBugged.FullRowSelect = true;

// Display grid lines.
lbxBugged.GridLines = true;

// Sort the items in the list in ascending order.
lbxBugged.Sorting = SortOrder.Ascending;

// Attach Subitems to the ListView

lbxBugged.Columns.Add("Code", 300, HorizontalAlignment.Left);
lbxBugged.Columns.Add("Description", 200, HorizontalAlignment.Left);
lbxBugged.Columns.Add("Author", 120, HorizontalAlignment.Left);
}

我试过:

最初使用的是列表框,但我已经读过,对于多列,则必须使用 ListView 。所以我已经切换到 ListView ,但它不会填充列表。不太确定哪种方法最好。

我已经初始化了 ListView ,所以它显示了标题,我只需要知道如何用相应的数据库信息填充这些空间。

populateListBox(从我使用列表框开始,我一直在研究,但只能发现人们使用来自实际数据库而非紧凑型数据库的数据库信息填充 ListView 。)

public void populateListBox()
{
String query = "SELECT Bug_Code, Bug_Description, Bug_Author FROM tblBugs";
SqlCeCommand mySqlCommand = new SqlCeCommand(query, mySqlConnection);
try
{
mySqlConnection.Open();
SqlCeDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
lbxBugged.Items.Clear();
while (mySqlDataReader.Read())
{
lbxBugged.Items.Add(mySqlDataReader["Bug_Code"].ToString() + " " + mySqlDataReader["Bug_Description"].ToString() + " " + mySqlDataReader["Bug_Author"].ToString());
}
}
catch (SqlCeException)
{
MessageBox.Show("Error populating list box");
}
}

最佳答案

第一次使用 Listview 忘记使用项目和子项目。这是我为其他挣扎的人完成的方法:

public void populateListView()
{
lbxBugged.Items.Clear();
SqlCeCommand cm = new SqlCeCommand("SELECT Bug_ID, Bug_Code, Bug_Description, Bug_Author FROM tblBugs ORDER BY Bug_ID ASC", mySqlConnection);

try
{
mySqlConnection.Open();
SqlCeDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem item = new ListViewItem(dr["Bug_ID"].ToString());
item.SubItems.Add(dr["Bug_Code"].ToString());
item.SubItems.Add(dr["Bug_Description"].ToString());
item.SubItems.Add(dr["Bug_Author"].ToString());

lbxBugged.Items.Add(item);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}


}

关于c# - 在 ListView 中拉取和拆分数据库项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com