c# - 如何根据数据库结果动态添加面板
问题描述
我想在面板中显示这个 datagridview 的结果,但是面板应该根据结果动态地相邻添加。就像我在 datagridview 中有 3 个结果一样,因此应该动态添加 3 个面板。请指教。
解决方案
您可以使用 aSqlConnection
执行SELECT
命令并获取所需的行并为每一行添加一个包含该行信息的自定义面板来实现此目的。
我还建议您使用 aFlowLayoutPanel
以便它为您处理间距/位置。
在你的Form_Load
:
var conn = new SqlConnection("Your connection informations here");
conn.Open();
var command = new SqlCommand("Select * from YourTable", conn);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// Pass the useful informations to your panel
var pnl = new MyCustomPanel(reader["Id"].ToString(), reader["Property_Type1"].ToString());
flowLayoutPanel1.Controls.Add(pnl);
}
}
conn.Close();
并且您的自定义控件的构造函数应该像这样,您可以在其中为每一行设置面板控件的值:
public MyCustomPanel(string id, string propertyType1)
{
txtId.Text = id;
txtPropertyType1.Text = propertyType1;
}
推荐阅读
- javascript - javascript:搜索包括 html 在内的文本并更改其 css 样式
- ios - JSQMessageViewController textview 不显示链接
- node.js - AWS Lambda 构造函数错误“找不到 API 配置 lambda-2015-03-31”
- powershell - 在文本文件中添加新的文本行
- android - 交错的相似回收者视图
- javascript - 带有数组和函数的待办事项列表
- linux - 移植 r5u870 驱动程序但 v4l2 合规性返回“VIDIO_QUERYCAP 失败,设备的 ioctl 不合适”
- javascript - 如何从另一个站点 src 获取文件大小?
- excel - 从数据库中查找重复数据
- vim - Yank 命令在 vim 编辑器中不起作用