`
ayaya
  • 浏览: 448989 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

winform datagridview 手动绑定列

阅读更多
/// <summary>
        /// Loads the user.
        /// </summary>
        /// <returns></returns>
        void BindDataGridView()
        {
            string strSQL = string.Empty;
            strSQL = "select a.UserId as userid,a.LoginId as loginid,a.UserName as username,a.UserLevel,b.LevelName from tbl_user a,tbl_userLevel b where userstatus =  1 and a.userLevel = b.LevelId ";
            ds = ca.ReturnDataSet(strSQL);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            //clear all columns
            this.dataGridView1.Columns.Clear();
            // bind column
            DisplayCol(dataGridView1, "userid", "用户编码");
            DisplayCol(dataGridView1, "username", "用户名");
        }


        /// <summary>
        /// Displays the col.
        /// </summary>
        /// <param name="dgv">The DGV.</param>
        /// <param name="dataPropertyName">Name of the data property.</param>
        /// <param name="headerText">The header text.</param>
        void DisplayCol(DataGridView dgv, String dataPropertyName, String headerText)
        {
            dgv.AutoGenerateColumns = false;
            DataGridViewTextBoxColumn obj = new DataGridViewTextBoxColumn();
            obj.DataPropertyName = dataPropertyName;
            obj.HeaderText = headerText;
            obj.Name = dataPropertyName;
            obj.Resizable = DataGridViewTriState.True;
            dgv.Columns.AddRange(new DataGridViewColumn[] { obj });
        }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics