方法一:
DataGridView gvshow = new DataGridView();//new一个grideview
gvshow.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.gvshow_DataBindingComplete);
DataTable dt = CreateSource(sqlStr); //获得数据源
gvshow.DataSource = dt;//设置数据源
this.Controls.Add(gvshow);
public DataTable CreateSource(string sqlStr)
{
DataSet ADataSet = new DataSet();
OleDbDataAdapter ADataAdapter = new OleDbDataAdapter(sqlStr, conn);
ADataAdapter.Fill(ADataSet);
return ADataSet.Tables[0];
}
private void gvshow_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//设置显示的列名
gvshow.Columns["RealName"].HeaderText = "姓名";
gvshow.Columns["LogTime"].HeaderText = "操作日期";
//设置列的宽度
gvshow.Columns["LogTime"].Width = 150;
gvshow.Columns["FormName"].HeaderText = "模块名称";
gvshow.Columns["FormName"].Width = 150;
gvshow.Columns["EventName"].HeaderText = "具体操作";
gvshow.Columns["UserIPAddress"].HeaderText = "IP地址";
gvshow.Columns["UserComputerName"].HeaderText = "计算机名称";
gvshow.Columns["UserComputerName"].Width = 150;
gvshow.Columns["CustomerID"].Visible = false;//隐藏某列:
}
方法二:
bind()中,selectCommand的select语句,指定中文名例:sslect stuId as 学号,stuName as 姓名 from stu.