
C#针对datagridview 操作收集的一些资料,内容比较乱,以后再整理
C#对DataGridView进行添加、修改、删除数据操作
用的是本地服务器(MySql):
设定全局变量:
MySqlConnection conn;
MySqlDataAdapter adapter;
MySqlTransaction trans;
1. // 联接
private System.Data.DataTable dbconn(string strSql)
{
string strconn = "host=localhost;database=test;user id=root;password=";
conn = new MySqlConnection();
conn.ConnectionString = strconn;
conn.Open();
this.adapter = new MySqlDataAdapter(strSql, conn);
System.Data.DataTable dtSelect = new System.Data.DataTable();
int rnt=this.adapter.Fill(dtSelect);
conn.Close();
return dtSelect;
}
2. //设定DataGridView的样式
private void setDgStyle()
{
this.dgselect.Columns.Clear();
DataGridViewCheckBoxColumn colDel = new DataGridViewCheckBoxColumn();
colDel.DataPropertyName = "Del";
colDel.Name = "Del";
colDel.Selected = false;
colDel.FalseValue = "0";
colDel.TrueValue = "1";
colDel.Width = 40;
colDel.SortMode = DataGridViewColumnSortMode.NotSortable;
colDel.HeaderText = "删除";
colDel.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colDel.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgselect.Columns.Insert(0, colDel);
DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
colID.DataPropertyName = "ProductsSpecID";
colID.Name = "ProductsSpecID";
colID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colID.HeaderText = "产品规ID";
colID.Width = 160;
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-50070-1.html
哦对了
和生产关系不大