在运行时更改流程布局面板中的控件顺序
Changing order of controls in flow layout panel during runtime
我有一个 Windows 表单,我在上面放置了流布局面板。我还有一个 class 读取本地数据库和 returns 相应的值。根据通过按钮的用户输入,面板会充满其他按钮。这些按钮的数量取决于本地数据库中的值。按钮显示正确且信息正确,但它们显示的顺序是按字母顺序排列的,即使数据库 class 中的数据 table 以正确的方式排序(通过来自数据库的 "ID" 列的数值)。
我还添加了一个数据网格视图来检查项目是否以正确的方式显示。我试图添加一个 for-each 循环,但这似乎只是随机化了按钮的顺序。
有谁知道如何让按钮以正确的方式显示,以便首先显示具有最低 "ID" 值的按钮。
下面是显示按钮的代码:
//set the datagridview with the correct values/names. Order works perfectly
dataGridView_AttackName.DataSource = db.attackIDName(attackCategory, taughtOn);
DataTable dt = db.attackIDName(attackCategory, taughtOn);
//sort datable again because doesnt work from db class
dt.DefaultView.Sort = "ID";
dt.DefaultView.ToTable();
int horizontal = 0;
int vertical = 0;
Button[] buttonArray = new Button[dt.Rows.Count];
for (int items = 0; items < buttonArray.Length; items++)
{
buttonArray[items] = new Button();
buttonArray[items].Size = new Size(150, 50);
buttonArray[items].Location = new Point(horizontal, vertical);
buttonArray[items].Name = string.Format("Button_{0}", dt.Rows[items]["ID"].ToString());
buttonArray[items].Text = dt.Rows[items]["Name"].ToString();
buttonArray[items].Click += btn_msg;
if ((items + 1) < buttonArray.Length)
{
vertical += 50;
}
flowLayoutPanel_AttackName.Controls.Add(buttonArray[items]);
}
//get the correct ID value from the button name and try to order it that way
foreach (Button b in flowLayoutPanel_AttackName.Controls)
{
string name = b.Name;
string subname = name.Substring(name.IndexOf("_") + 1);
int i = Convert.ToInt32(subname);
flowLayoutPanel_AttackName.Controls.SetChildIndex(b, i);
}
我在这个网站上四处搜索,但找不到任何有用的东西。
您已按 ID 对 DefaultView 进行了正确排序,但还没有使用结果!
您需要将 dt.Rows[items]["Name"]
替换为 dt.DefaultView[items].Row["Name"]
。然后你不需要语句 dt.DefaultView.ToTable()
.
完整代码如下:
dt.DefaultView.Sort = "ID";
int horizontal = 0;
int vertical = 0;
Button[] buttonArray = new Button[dt.Rows.Count];
for (int items = 0; items < buttonArray.Length; items++)
{
buttonArray[items] = new Button();
buttonArray[items].Size = new Size(150, 50);
buttonArray[items].Location = new Point(horizontal, vertical);
buttonArray[items].Name = string.Format("Button_{0}", dt.DefaultView[items].Row["ID"].ToString());
buttonArray[items].Text = dt.DefaultView[items].Row["Name"].ToString();
buttonArray[items].Click += btn_msg;
if ((items + 1) < buttonArray.Length)
{
vertical += 50;
}
flowLayoutPanel_AttackName.Controls.Add(buttonArray[items]);
}
我有一个 Windows 表单,我在上面放置了流布局面板。我还有一个 class 读取本地数据库和 returns 相应的值。根据通过按钮的用户输入,面板会充满其他按钮。这些按钮的数量取决于本地数据库中的值。按钮显示正确且信息正确,但它们显示的顺序是按字母顺序排列的,即使数据库 class 中的数据 table 以正确的方式排序(通过来自数据库的 "ID" 列的数值)。
我还添加了一个数据网格视图来检查项目是否以正确的方式显示。我试图添加一个 for-each 循环,但这似乎只是随机化了按钮的顺序。
有谁知道如何让按钮以正确的方式显示,以便首先显示具有最低 "ID" 值的按钮。
下面是显示按钮的代码:
//set the datagridview with the correct values/names. Order works perfectly
dataGridView_AttackName.DataSource = db.attackIDName(attackCategory, taughtOn);
DataTable dt = db.attackIDName(attackCategory, taughtOn);
//sort datable again because doesnt work from db class
dt.DefaultView.Sort = "ID";
dt.DefaultView.ToTable();
int horizontal = 0;
int vertical = 0;
Button[] buttonArray = new Button[dt.Rows.Count];
for (int items = 0; items < buttonArray.Length; items++)
{
buttonArray[items] = new Button();
buttonArray[items].Size = new Size(150, 50);
buttonArray[items].Location = new Point(horizontal, vertical);
buttonArray[items].Name = string.Format("Button_{0}", dt.Rows[items]["ID"].ToString());
buttonArray[items].Text = dt.Rows[items]["Name"].ToString();
buttonArray[items].Click += btn_msg;
if ((items + 1) < buttonArray.Length)
{
vertical += 50;
}
flowLayoutPanel_AttackName.Controls.Add(buttonArray[items]);
}
//get the correct ID value from the button name and try to order it that way
foreach (Button b in flowLayoutPanel_AttackName.Controls)
{
string name = b.Name;
string subname = name.Substring(name.IndexOf("_") + 1);
int i = Convert.ToInt32(subname);
flowLayoutPanel_AttackName.Controls.SetChildIndex(b, i);
}
我在这个网站上四处搜索,但找不到任何有用的东西。
您已按 ID 对 DefaultView 进行了正确排序,但还没有使用结果!
您需要将 dt.Rows[items]["Name"]
替换为 dt.DefaultView[items].Row["Name"]
。然后你不需要语句 dt.DefaultView.ToTable()
.
完整代码如下:
dt.DefaultView.Sort = "ID";
int horizontal = 0;
int vertical = 0;
Button[] buttonArray = new Button[dt.Rows.Count];
for (int items = 0; items < buttonArray.Length; items++)
{
buttonArray[items] = new Button();
buttonArray[items].Size = new Size(150, 50);
buttonArray[items].Location = new Point(horizontal, vertical);
buttonArray[items].Name = string.Format("Button_{0}", dt.DefaultView[items].Row["ID"].ToString());
buttonArray[items].Text = dt.DefaultView[items].Row["Name"].ToString();
buttonArray[items].Click += btn_msg;
if ((items + 1) < buttonArray.Length)
{
vertical += 50;
}
flowLayoutPanel_AttackName.Controls.Add(buttonArray[items]);
}