日期:2014-05-17  浏览次数:20712 次

dataGridView1 使用请教,如何加数据到默认列里去?

 private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.Add("col1", "empno");
            dataGridView1.Columns.Add("col2", "name");
            dataGridView1.Columns.Add("col3", "age");        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //连接字符串
            //string connectionString = "Data Source=orcl;User ID=system;Password=system;Unicode=True ";
            string connectionString = "Data Source=orcl;User ID=scott;Password=scott;Unicode=True";
            //创建Oracle连接对象
            OracleConnection con = new OracleConnection(connectionString);
            //打开连接
            con.Open();

            //建立Sql查询语句
            string sql = "select * from emp";
            //创建Oracle命令行对象
            OracleCommand cmd = new OracleCommand(sql, con);
            //执行命令行对象
            OracleDataReader odr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            IList<UserInfo> users = new List<UserInfo>();
            while (odr.Read())
            {
                UserInfo user = new UserInfo();
                user.UserID = Convert.ToInt32(odr["EMPNO"]);
                user.UserName = Convert.ToString(odr["ENAME"]);
                user.UserAge = Convert.ToString(odr["sal"]);
                users.Add(user);
            }
            //绑定DataGridView控件
            dataGridView1.DataSource = users;