您的位置:首页 > 其它

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

2016-12-19 14:49 232 查看
 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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐