c#数据显示问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 07:31:50
问题以前问过,不过没弄懂. 数据库"表格"里有"表一",表一里有一列数据,time控件设置的是1秒,我希望每一秒,表一中那列数字在文本框中轮流出现, 同一个控件每秒显示一个数据;下面是time控件中的代码,这样只显示最后一个数字,怎么改? 请写代码,谢谢,试过能用加分!
private void timer1_Tick(object sender, EventArgs e)
{
SqlConnection thisConnection = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=总表");

SqlDataAdapter dgAdapter = new SqlDataAdapter("SELECT 数列 from 表一", thisConnection);

DataSet dg = new DataSet();

dgAdapter.Fill(dg, "表一");

for ( int i = 0 ; i < dg.Tables["表一"].Rows.Count; i++)
this.txtdg.Text = float.Parse(dg.Tables["表一"].Rows[i][0].ToString()).ToString("0.000");
}

上面当执行timer1_Tick事件时它就从第一行,显示到就后一行,当然只看得到最后一行了,你把它改成每执行一次读取指定一行就可以了,X代表行号逐渐增加,最最后一行时又从0开始,就达到你目的了.
int x = 0;
DataSet dg = new DataSet();
private void Form_Load(object sender, EventArgs e)
{
SqlConnection thisConnection = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=总表");
SqlDataAdapter dgAdapter = new SqlDataAdapter("SELECT 数列 from 表一", thisConnection);

dgAdapter.Fill(dg, "表一");
}

private void timer1_Tick(object sender, EventArgs e)
{
this.txtdg.Text = float.Parse(dg.Tables["表一"].Rows[x][0].ToString()).ToString("0.000");
if (x < dg.Tables["表一"].Rows.Count)
x++;
else
x = 0;
}