欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

winform datagridview 手动绑定列

程序员文章站 2022-03-04 11:51:26
...

/// <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 });
}
相关标签: WinForm