Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文本框与标签匹配_C#_Winforms_Textbox_Label - Fatal编程技术网

C# 将文本框与标签匹配

C# 将文本框与标签匹配,c#,winforms,textbox,label,C#,Winforms,Textbox,Label,在运行时,我查询一个访问表并将结果存储在一个数组中。例如,访问表的数据结构是 SalespersonName ------ Item1Sold ----- Item2Sold ---------- Item3Sold Mitch Boat Camera Jason Car Shells Mitch Eggs Richard

在运行时,我查询一个访问表并将结果存储在一个数组中。例如,访问表的数据结构是

SalespersonName ------ Item1Sold ----- Item2Sold ---------- Item3Sold
Mitch                  Boat            Camera
Jason                  Car             Shells
Mitch                  Eggs
Richard                Coffee          Bacon                 Beans
我想创建一个包含salersonname的标签&在salersonname旁边创建一个文本框,显示所有非空itemsaled字段。所以我想要的输出是

(label) Mitch (textbox) Boat (textbox) Camera
(label) Jason (textbox) car (textbox) shells
(label) Mitch (textbox) Eggs
(label) Richard (textbox) Coffee (textbox) bacon (tetbox) beans
我不知道如何仅在值不为null时生成文本框。我只能为所有人或任何事这样做,这不是我所追求的

代码添加下面的所有内容

string path = "A:\\Database1.mdb";
object oName = path;
System.Collections.Hashtable lookup = new System.Collections.Hashtable();
OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +  oName);
olecon.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM info", olecon);
dr = command.ExecuteReader();
while (dr.Read())
{
   fields.Add(dr[0].ToString());
}
dr.Close();
foreach (string field in fields)
{
   Label labelz = new Label();
   labelz.Name = "labelz_" + index;
   labelz.Text = label;
   labelz.AutoSize = true;
   labelz.Location = new Point(10, 30);
   panel1.Controls.Add(labelz);

   TextBox textboxes = new TextBox();
   textboxes.Name = "txt_" + index;
   textboxes.Location = new Point(10, 80);
   textboxes.AutoSize = true;
   panel1.Controls.Add(textboxes);
   if (field != "")
   {
     panel1.SetFlowBreak(textboxes, true);
   }

   index++;
}

您的代码没有添加所有组件。它在标签变量和文本框中添加带有文本的标签。我建议您将逻辑分为两部分:

  • 检索数据并将其保存在更易于操作的结构中—创建一个类来保存数据库中所需的数据
  • 使用数据在所需布局中创建零部件
如何实现(未测试伪代码):


第一部分:

//data class
class Person
{
  public string Name {get;set;}
  public List<string> SoldItems {get;set;}
}

//fill data
List<Person> Persons = new List<Person>();
int itemColumnCount = 3;
while (dr.Read())
{
  Person person = new Person();
  person.SoldItems = new List<string>();
  //Get person name
  person.Name = dr.GetString(dr.GetOrdinal("personNameColumn"));
  //Get sold items
  for(int i = 1; i <= itemColumnCount; i++)
  {
    //lets say you have columns: ItemSoldColumn1, ItemSoldColumn2, ItemSoldColumn3 for example
    string columnName = "ItemSoldColumn" + i;
    //check if column value is not null
    if(!dr.IsDBNull(dr.GetOrdinal(columnName)))
    {
      string soldItem = dr.GetString(dr.GetOrdinal(columnName));
      //add to persons sold items list
      person.SoldItems.Add(soldItem);
    }
  }
}

另外,请考虑将数据库结构更改为Person--请向您的代码展示您如何处理所有人的问题?这将更容易为您指明可以进行修复的位置。@Reniuz code添加所有已添加的内容。为此,请使用
DataGridView
@我可以看到使用DataGridView如何使返回的数据更干净,但仍然返回到我原来的状态?这将如何使文本框关联工作?我是否必须首先查询以获取项目列计数,因为这在最初是未知的?是的。但是为了更加优雅(添加重复列不是好的设计),创建两个表,一个用于存储人员,另一个用于存储已售出的物品。我只是想知道如何获取itemColumnCount的计数。我如何在不首先查询数据库的情况下获得该信息?datareader中有一个属性,它返回所有列计数。然后,您可以减去最初已知的列的数量,或者您可以循环遍历所有列名,只取名称开头的列,例如“itemsaled”。
foreach(var person in Persons)
{
   //create label and set text to persons name
   //...
   newLabel.Text = person.Name;
   //add to panel or some kind of layout control

   //here you will have only sold items which is not null in database
   //so just loop through sold items list and create texboxes   
   foreach(var item in person.SoldItems)
   {
     //create textbox
     newTextBox.Text = item;
     //add to panel or some kind of layout control
   }
}