0
点赞
收藏
分享

微信扫一扫

winform datagridview 绑定


1.DataGridView绑定DataTable

/// <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 });
}


2.绑定List

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.UI.WebControls;

namespace X.WinFormsApp
{
/// <summary>
/// 标题:DataGridView 简单绑定 List 对像
/// 日期:2009-05-14
/// </summary>
public partial class X200905142324 : FormBase
{
public X200905142324()
{
InitializeComponent();
//this.OptionsDescription.Title = "DataGridView 简单绑定 List 对像";
this.BindGrid();
}

private void BindGrid()
{
List<XList> fXList = new List<XList>();

fXList.Add(new XList(1, "Rol", true));
fXList.Add(new XList(2, "Chole", false));
fXList.Add(new XList(3, "John", true));
fXList.Add(new XList(4, "X.X.Y", false));

this.xGrid1.AutoGenerateColumns = false;

DataGridViewColumn fColumn1 = new DataGridViewColumn(new DataGridViewTextBoxCell());
fColumn1.HeaderText = "Id";
fColumn1.DataPropertyName = "Id";

DataGridViewColumn fColumn2 = new DataGridViewColumn(new DataGridViewTextBoxCell());
fColumn2.HeaderText = "Name";
fColumn2.DataPropertyName = "Name";

DataGridViewColumn fColumn3 = new DataGridViewColumn(new DataGridViewCheckBoxCell());
fColumn3.HeaderText = "IsDeleted";
fColumn3.DataPropertyName = "IsDeleted";

// 这里改变 Add 顺序将会改变列显示顺序
this.xGrid1.Columns.Add(fColumn1);
this.xGrid1.Columns.Add(fColumn2);
this.xGrid1.Columns.Add(fColumn3);

this.xGrid1.DataSource = fXList;
}
}

public class XList
{
private int fId;
private string fName;
private bool fIsDeleted;

public int Id
{
get { return fId; }
set { fId = value; }
}

public string Name
{
get { return fName; }
set { fName = value; }
}

public bool IsDeleted
{
get { return fIsDeleted; }
set { fIsDeleted = value; }
}

public XList(int fId, string fName, bool fIsDeleted)
{
this.fId = fId;
this.fName = fName;
this.fIsDeleted = fIsDeleted;
}
}
}

举报

相关推荐

0 条评论