winform 绑定数据 dataGridView

 ///


        /// 绑定数据
        ///

        public void   bdData()
        {
            string connstr = ConfigurationManager.ConnectionStrings["licai_MrConnectionString"].ToString();
            OleDbConnection conn = new OleDbConnection(connstr);
            conn.Open();
            //MessageBox.Show(conn.State.ToString());
            string sql = "select * from Mr_xflx_Class ";
            //创建DataAdapter对象,用来连接sql语句和连接数据库语句.
           // OleDbCommand cmd = new OleDbCommand(sql, conn);

            OleDbDataAdapter da = new OleDbDataAdapter(sql,conn);// (cmd);
            //在创建一个DataSet对象
            DataSet ds = new DataSet();

            //在用DataAdapter的Fill对象添加数据.
            da.Fill(ds);

//修改名称
            ds.Tables[0].Columns["id"].ColumnName = "ID";
            ds.Tables[0].Columns["xflx"].ColumnName = "消费类型";
            ds.Tables[0].Columns["Parentid"].ColumnName = "父ID";
            dataGridView1.DataSource = ds.Tables[0].DefaultView;//指定数据源ds.Tables[0].DefaultView;
            conn.Close();
        }

你可能感兴趣的:(.net,Winform)