如何添加SqlDataReader返回通用List的值?我有一个方法,我SqlDataReader用来CategoryID从Category表中获取.我想添加所有CategoryID通用列表.
这个剂量不起作用,因为它只返回一个categoryID,这是最后一个.我想将所有内容添加categoryID到列表中,然后返回它们.
我怎么做?
SqlConnection connection = null;
SqlDataReader reader = null;
SqlCommand cmd = null;
try
{
connection = new SqlConnection(connectionString);
cmd = new SqlCommand("select CategoryID from Categories", connection );
connection.Open();
List<int> catID = new List<int>();
dr = cmd.ExecuteReader();
while (dr.Read())
{
catID.Add(Convert.ToInt32(dr["CategoryID"].ToString()));
}
}
finally
{
if (connection != null)
connection.Close();
}
return catID;
Run Code Online (Sandbox Code Playgroud) 我有一个Windows窗体应用程序,我在datagridview中显示有关产品和产品类别的信息.我想创建一个弹出窗口,这样当我右键单击某个产品并选择添加到类别时,会出现一个弹出窗口,并且我会在下拉框中显示所有类别,当我单击按钮时,该产品会添加到该类别.
我想创建一个带有下拉框和按钮的弹出窗口.我如何在窗体应用程序中执行此操作?
我有一个TreeView和一个上下文菜单.我只想在右键单击ROOT节点而不是子节点时显示上下文菜单.
这就是我到目前为止所拥有的.即使我右键单击子节点,也会显示"上下文菜单".如何更改此设置,以便仅在我右键单击根节点时才显示菜单?可能吗?
if(e.Button == MouseButtons.Right)
{
// Select the clicked node
treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
if(treeView1.SelectedNode != null)
{
myContextMenuStrip.Show(treeView1, e.Location)
}
}
Run Code Online (Sandbox Code Playgroud)