1.首先拉个GridControl控件
2.绑定数据字段点击=》run denger=>选择cloums,添加字段并设置属性:
Name:名称
caption:字段别名
fieldName:绑定数据库的字段(跟数据库的字段一样)
=》close(退出)
注:前面复选框:run denger=>view属性=》Optionsselection=>MultiSelectMode改为CheckBoxRowSelect ,MultiSelect改为true
3.代码
数据库连接类:
constr为连接字符串,
复制的就是constr里面的字符串
public class SqlHelper
{
/// <summary>
/// 数据库链接
/// </summary>
public const string constr = "Data Source=.;Initial Catalog=Factory;Integrated
Security=True;Encrypt=False";
/// <summary>
/// 查询
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataTable Select(string sql)
{
SqlConnection coon=new SqlConnection(constr);
coon.Open();
DataTable dt = new DataTable();
SqlDataAdapter add=new SqlDataAdapter(sql,coon);
add.Fill(dt);
coon.Close();
return dt;
}
/// <summary>
///增删改
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public bool Excute(string sql)
{
SqlConnection coon = new SqlConnection(constr);
coon.Open();
SqlCommand cmd = new SqlCommand(sql,coon);
int a =cmd.ExecuteNonQuery();
coon.Close() ;
return a > 0;
}
}
最后全局调用
public void Select()
{
string sql = "select*from Custormer";//查询数据sql
SqlHelper sqlHelper = new SqlHelper();//创建数据库连接实列
DataTable dt = sqlHelper.Select(sql);//调用数据库查询方法,存在datatable里面
this.gridControl1.DataSource = dt;//给gridcontroler绑定数据
}
启动项目,数据就出来了