C#foreach循环为什么是倒序

.Net技术 码拜 9年前 (2016-03-10) 2644次浏览
本人连接了数据库
数据库中 假如桌台01 – 06 分别是 000111 在TEXTBOX里面输出就是111000
不知道怎么回事求指导释~
SqlConnection con = new SqlConnection(“server=localhost;Database=cygl;uid=yg1;pwd=123”);
SqlDataAdapter sda = new SqlDataAdapter(“select * from ztb”, con);
DataSet ds = new DataSet();
sda.Fill(ds);
int x=0;

foreach (Control c in this.Controls)
{
if (c is TextBox)
{

string val = ds.Tables[0].Rows[x++][2].ToString();
c.Text = val;
}
}

解决方案

5

没有倒序的操作。
设置断点,单步调试,看每步val的值究竟是什么。

5

this.Controls,获取到的控件,应该是按你添加到Form里面的顺序排列的

5

foreach (Text c in this.Controls.OfType<TextBox>().OrderBy(x => x.Name))

5

这有什么可奇怪的?
你看看 Form1.Designer.cs 只知道

            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.textBox6 = new System.Windows.Forms.TextBox();

这样定义的控件,是这样加入到控件数组的

            this.Controls.Add(this.textBox6);
            this.Controls.Add(this.textBox5);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);

因此,假如你计划利用Controls 来做些事情,就该本人书写代码,而不是指望 VS


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#foreach循环为什么是倒序
喜欢 (0)
[1034331897@qq.com]
分享 (0)