c#如何从数据库表内读取内容并动态创建CheckBox

c#如何从数据库表内读取内容并动态创建CheckBox表名:abcd表abcd里面有:ID,BuMen根据BuMen的数量,动态创建CheckBox,CheckBox的t... c#如何从数据库表内读取内容并动态创建CheckBox
表名:abcd
表abcd里面有:ID,BuMen
根据BuMen的数量,动态创建CheckBox,CheckBox的text为BuMen的值

然后在写入到数据库表aa的字段bb中
还有,动态创建的CheckBox的坐标如何定义

因为grouppanel里面已经有两行控件,想放到这两行控件的下方
展开
 我来答
惠赞悦2c
2012-05-04 · 超过33用户采纳过TA的回答
知道答主
回答量:119
采纳率:0%
帮助的人:67.9万
展开全部

public partial class Form1 : Form

    {

        private SqlConnection conn = null;

        public Form1()

        {

            InitializeComponent();

            conn = new SqlConnection();

            conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\database\loginDemo\loginDemo\bin\Debug\demo.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";

        }

        private void gpbMain_Enter(object sender, EventArgs e)

        {

            conn = getConnection();

            SqlCommand comm = conn.CreateCommand();

            comm.CommandText = "select bumen from abcd ";

            comm.CommandType = CommandType.Text;

            SqlDataReader read = comm.ExecuteReader();

            int x = txtMarry.Location.X;

            int y = txtMarry.Location.Y;

            while (read.Read())

            {

                y += 20;

                if (gpbMain.Width < y)

                {

                    this.Width += 20;

                    gpbMain.Width += 20;

                }

                CheckBox ch = new CheckBox();

                ch.Location = new Point(x, y);

                ch.Text = read.GetString(0).ToString();

                gpbMain.Controls.Add(ch);

            }

            if (conn != null)

            {

                conn.Close();

                conn.Dispose();

            }

        }

        private SqlConnection getConnection()

        {

            if (conn.State == ConnectionState.Closed)

            {

                conn.Open();

            }

            return conn;

        }

    }

}

数据库就是一个Id,bumen.

枪口下的兔子
2015-09-26 · TA获得超过1075个赞
知道小有建树答主
回答量:1364
采纳率:0%
帮助的人:655万
展开全部
public partial class Form1 : Form
{
private SqlConnection conn = null;
public Form1()
{
InitializeComponent();
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\database\loginDemo\loginDemo\bin\Debug\demo.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
}
private void gpbMain_Enter(object sender, EventArgs e)
{
conn = getConnection();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select bumen from abcd ";
comm.CommandType = CommandType.Text;
SqlDataReader read = comm.ExecuteReader();
int x = txtMarry.Location.X;
int y = txtMarry.Location.Y;
while (read.Read())
{
y += 20;
if (gpbMain.Width < y)
{
this.Width += 20;
gpbMain.Width += 20;
}
CheckBox ch = new CheckBox();
ch.Location = new Point(x, y);
ch.Text = read.GetString(0).ToString();
gpbMain.Controls.Add(ch);
}
if (conn != null)
{
conn.Close();
conn.Dispose();
}
}
private SqlConnection getConnection()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式