c#中遍历数据的最佳方法是什么?
本文关键字:方法 是什么 最佳 遍历 数据 | 更新日期: 2023-09-27 17:54:39
我想循环遍历设备应用程序上的一些数据。例如:
Select name, surname, mail_ad, phone_nr from cust_details
在表单上,我想用下一个和上一个按钮逐行显示。例如:
名称: 沃纳
Surename: VDH
邮件:werner@me.com
电话号码: 0716848805
(以前) <[下]strong>
我在这方面一直有很多麻烦。我以前使用过列表,但它不像我想要它工作。有人能帮我一下吗?
如何使用列表:
public List<String> InfoList = new List<String>();
int i = 1;
conn.Open();
string query;
query = "Select name, surname, mail_ad, phone_nr from cust_details";
OracleCommand cmd = new OracleCommand(query, conn);
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.InfoList.Add(dr["name"].ToString());
this.InfoList.Add(dr["surname"].ToString());
this.InfoList.Add(dr["mail_ad"].ToString());
this.InfoList.Add(dr["phone_nr "].ToString());
}
dr.Close();
conn.Close();
,然后在"下一步"answers"上一步"按钮中:
if (i + 1 < this.InfoList.Count)
label1.Text = this.InfoList[++i];
if (i + 1 < this.InfoList.Count)
label2.Text = this.InfoList[++i];
if (i + 1 < this.InfoList.Count)
label3.Text = this.InfoList[++i];
if (i + 1 < this.InfoList.Count)
label4.Text = this.InfoList[++i];
if (i - 1 < this.InfoList.Count)
label1.Text = this.InfoList[--i];
if (i - 1 < this.InfoList.Count)
label2.Text = this.InfoList[--i];
if (i - 1 < this.InfoList.Count)
label3.Text = this.InfoList[--i];
if (i - 1 < this.InfoList.Count)
label4.Text = this.InfoList[--i];
我使用标签来显示数据。我的问题是:有时当我点击下一个时,标签的顺序会混淆。或者1个标签里面没有任何信息。当我点击previous,没有更多的细节,我得到异常
我不知道这是否是您所寻找的,但只需创建一个类,然后使用List<YourClass>
。使用List<YourClass> myList = new List<YourClass>();
要向List添加值,您必须创建一个YourClass
对象并在那里定义值:
YourClass myClassObject = new YourClass();
myClassObject.name = dr[i].toString(); //This is the database reader
Class YourClass
包含以下属性:
string name,
string surname,
string mail_address,
string phone_nr.
属性是这样写的,表现得像全局变量:
internal string mystring {get; set;}
然后你可以循环遍历这个列表并使用以下变量:
name_Lbl.Text = this.myList.name[i]; //This is an example.
然后使用按钮的OnPress-Event来增加或减少i
。
编辑:正确命名你的类;)
编辑2:有关属性的更多信息,请单击ME
把它变成一个类:
class CustDetails
public string Name
{
get;
set;
}
public string Surname
{
get;
set;
}
public string Email
{
get;
set;
}
public ulong PhoneNumber
{
get;
set;
}
步骤1,将您的详细信息封装到一个数据包类
public class CustDetails
{
public string Name {get; set;}
public string Surname {get; set;}
public string MailAddress {get; set;]
public string Phone {get; set;}
}
步骤2。创建CustDetails列表——这是您的InfoList
public List<CustDetails> InfoList = new List <CustDetails>();
步骤3。将查询的详细信息分配给新类
conn.Open();
string query = "Select name, surname, mail_ad, phone_nr from cust_details";
OracleCommand cmd = new OracleCommand(query, conn);
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var details = new CustDetails { Name = dr["name"].ToString(),
Surname = dr["surname"].ToString(),
MailAddress = dr["mail_ad"].ToString(),
Phone = dr["phone"].ToString() }
this.InfoList.Add(details);
}
dr.Close();
conn.Close();
步骤4。记住你现在的位置。实现方法以访问上一个和下一个按钮中的上一个或下一个详细信息。您的上一个和下一个按钮处理程序分别如下所示:
//previous
if (current == 0)
//you are at the beginning of the list, the button should be disabled,
// handle it accordingly. In this case I just terminate
return;
current--;
var detail = InfoList[current];
label1.Text = detail.Name;
label2.Text = detail.Surname;
label3.Text = detail.MailAddress;
label4.Text = detail.Phone;
//next
if (current == (InfoList.Count -1))
//you are at the endof the list, the button should be disabled,
// handle it accordingly. In this case I just terminate
return;
current++;
var detail = InfoList[current];
label1.Text = detail.Name;
label2.Text = detail.Surname;
label3.Text = detail.MailAddress;
label4.Text = detail.Phone;
只需确保您可以跟踪方法调用之间的"当前"要么将其作为参数添加,要么创建一个全局变量