winform 动态加载表以按钮形式显示机器状态

来源:互联网 发布:普通话练习软件下载 编辑:程序博客网 时间:2024/05/22 16:55
 DataSet ds = new DataSet();


            using (SqlConnection conn = new SqlConnection("Data Source=192.168.1.234;Initial Catalog=wiktest;User ID=sa;Password=*****"))
            {
                SqlCommand comm=new SqlCommand("select id,name,des,zt from machine", conn);
                comm.CommandType = CommandType.Text;
                
                SqlDataAdapter da = new SqlDataAdapter(comm);
                da.Fill(ds, "UserTable");
            }
            int mint = ds.Tables[0].Rows.Count;
            int row = 0;
            string s = "test";
            //bool zt;
            for (int rr=0;rr<mint;rr++)
            {
                if (rr % 10 == 0 && rr != 0)
                {
                    row++;
                }
                Button btn = new Button();
                //控件名称
                btn.Name = "mybutton" + rr.ToString();
                //控件显示文本
                btn.Text = string.Format("机器{0}", ds.Tables[0].Rows[rr][0].ToString());
                s = ds.Tables[0].Rows[rr][2].ToString();
                if (ds.Tables[0].Rows[rr][3].ToString() == "True")
                    btn.BackColor = Color.Red;
                else
                    btn.BackColor = Color.Blue;


                //控件大小
                btn.Size = new Size(50, 50);
                //控件位置【动态变化】
                btn.Location = new Point(50 + rr % 10 * 100, 50 + row * 100);
                //添加到窗体
                this.Controls.Add(btn);
                ToolTip msg = new ToolTip();
                msg.ShowAlways = true;
                msg.SetToolTip(btn, s);
0 0