如何以代码方式创建gridview,并将其绑定数据(C#)
2个回答
2013-09-19
展开全部
这是个简易的代码演示怎么使用,一些其他的属性设置请自己查阅MSDNDataGridView gview = new DataGridView(); //实例化控件,最好是全局
gview.Dock = DockStyle.Fill;
this.Controls.Add(gview); //添加GRIDVIEW控件到指定的窗体,这里是直接添加到窗体
gview.DataSource = 数据源 //关键设置数据库,数据源就可以了
gview.Dock = DockStyle.Fill;
this.Controls.Add(gview); //添加GRIDVIEW控件到指定的窗体,这里是直接添加到窗体
gview.DataSource = 数据源 //关键设置数据库,数据源就可以了
2013-09-19
展开全部
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); //初始化DataGridView
DataGridViewTextBoxColumn column; //添加复选框
DataGridViewCheckBoxColumn ColumnCheck = new DataGridViewCheckBoxColumn();
ColumnCheck.HeaderText = "选择";
ColumnCheck.Name = "DeleteSelect";
ColumnCheck.Resizable = System.Windows.Forms.DataGridViewTriState.True;
ColumnCheck.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
ColumnCheck.Width = 40;
this.dataGridView1.Columns.Add(ColumnCheck); column = new DataGridViewTextBoxColumn();
column.Name = "id";
column.HeaderText = "id";
//设置绑定数据源的barCode字段
column.DataPropertyName = "id";
column.Visible = false;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "barCode";
column.HeaderText = "商品条码";
//设置绑定数据源的barCode字段
column.DataPropertyName = "barCode";
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "ProductsName";
column.HeaderText = "商品名称";
//设置绑定数据源的ProductsName字段
column.DataPropertyName = "ProductsName";
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "Price";
column.HeaderText = "价格";
//设置绑定数据源的Price字段
column.DataPropertyName = "Price";
this.dataGridView1.Columns.Add(column); //添加修改按钮 DataGridViewLinkColumn btn = new DataGridViewLinkColumn();
btn.Name = "btnEdit";
btn.HeaderText = "修改";
btn.DefaultCellStyle.NullValue = "修改";
btn.Width = 80;
btn.UseColumnTextForLinkValue = true;
dataGridView1.Columns.Add(btn);
//设置不自动产生列
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.AllowUserToAddRows = false; Main Main = new Main();
Main.Visible = false; Bind();
this.dataGridView1.Columns[0].Width = 70;
this.dataGridView1.Columns[this.dataGridView1.Columns.Count - 1].Width = 70;
this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); //初始化DataGridView
DataGridViewTextBoxColumn column; //添加复选框
DataGridViewCheckBoxColumn ColumnCheck = new DataGridViewCheckBoxColumn();
ColumnCheck.HeaderText = "选择";
ColumnCheck.Name = "DeleteSelect";
ColumnCheck.Resizable = System.Windows.Forms.DataGridViewTriState.True;
ColumnCheck.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
ColumnCheck.Width = 40;
this.dataGridView1.Columns.Add(ColumnCheck); column = new DataGridViewTextBoxColumn();
column.Name = "id";
column.HeaderText = "id";
//设置绑定数据源的barCode字段
column.DataPropertyName = "id";
column.Visible = false;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "barCode";
column.HeaderText = "商品条码";
//设置绑定数据源的barCode字段
column.DataPropertyName = "barCode";
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "ProductsName";
column.HeaderText = "商品名称";
//设置绑定数据源的ProductsName字段
column.DataPropertyName = "ProductsName";
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "Price";
column.HeaderText = "价格";
//设置绑定数据源的Price字段
column.DataPropertyName = "Price";
this.dataGridView1.Columns.Add(column); //添加修改按钮 DataGridViewLinkColumn btn = new DataGridViewLinkColumn();
btn.Name = "btnEdit";
btn.HeaderText = "修改";
btn.DefaultCellStyle.NullValue = "修改";
btn.Width = 80;
btn.UseColumnTextForLinkValue = true;
dataGridView1.Columns.Add(btn);
//设置不自动产生列
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.AllowUserToAddRows = false; Main Main = new Main();
Main.Visible = false; Bind();
this.dataGridView1.Columns[0].Width = 70;
this.dataGridView1.Columns[this.dataGridView1.Columns.Count - 1].Width = 70;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询