在使用vs2010 给datagridview 绑定数据的时候,如果设置gridview 的 colums 集合的话,数据能绑定上去,比如绑定10列数据,有10列,但是这10列的数据都是空的。数据不显示。这个如何解决~!这个是cs文件内的绑定数据的方法: public void CreateTb()
{
DataTable tb = new DataTable();
tb.Columns.Add(new DataColumn("id"));
tb.Columns.Add(new DataColumn("name"));
tb.Columns.Add(new DataColumn("text"));
tb.Columns.Add(new DataColumn("remk")); DataRow row = null; for (int i = 0; i < 10; i++)
{
row = tb.NewRow();
row["id"] = i.ToString();
row["name"] = "name"+i.ToString();
row["text"] = "text"+i.ToString();
row["remk"] = "remk"+i.ToString();
tb.Rows.Add(row);
}
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.DataSource = tb.DefaultView;
}
这个是设计器内部的代码: private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.id = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.name = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.text = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.remk = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.form1BindingSource = new System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(584, 122);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.id,
this.name,
this.text,
this.remk});
this.dataGridView1.Location = new System.Drawing.Point(2, 1);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(530, 194);
this.dataGridView1.TabIndex = 1;
//
// id
//
this.id.HeaderText = "id";
this.id.Name = "id";
this.id.ReadOnly = true;
//
// name
//
this.name.HeaderText = "name";
this.name.Name = "name";
this.name.ReadOnly = true;
//
// text
//
this.text.HeaderText = "text";
this.text.Name = "text";
this.text.ReadOnly = true;
//
// remk
//
this.remk.HeaderText = "remk";
this.remk.Name = "remk";
this.remk.ReadOnly = true;
//
// form1BindingSource
//
this.form1BindingSource.DataSource = typeof(TestWindowsForm.Form1);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(747, 382);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).EndInit();
this.ResumeLayout(false); }
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn id;
private System.Windows.Forms.DataGridViewTextBoxColumn name;
private System.Windows.Forms.DataGridViewTextBoxColumn text;
private System.Windows.Forms.DataGridViewTextBoxColumn remk;
private System.Windows.Forms.BindingSource form1BindingSource;如果不设置 dataGridView 的DataGridViewTextBoxColumn ,这个就能绑定数据,设置了 有数据,但是看到的数据内容为空!
{
DataTable tb = new DataTable();
tb.Columns.Add(new DataColumn("id"));
tb.Columns.Add(new DataColumn("name"));
tb.Columns.Add(new DataColumn("text"));
tb.Columns.Add(new DataColumn("remk")); DataRow row = null; for (int i = 0; i < 10; i++)
{
row = tb.NewRow();
row["id"] = i.ToString();
row["name"] = "name"+i.ToString();
row["text"] = "text"+i.ToString();
row["remk"] = "remk"+i.ToString();
tb.Rows.Add(row);
}
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.DataSource = tb.DefaultView;
}
这个是设计器内部的代码: private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.id = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.name = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.text = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.remk = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.form1BindingSource = new System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(584, 122);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.id,
this.name,
this.text,
this.remk});
this.dataGridView1.Location = new System.Drawing.Point(2, 1);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(530, 194);
this.dataGridView1.TabIndex = 1;
//
// id
//
this.id.HeaderText = "id";
this.id.Name = "id";
this.id.ReadOnly = true;
//
// name
//
this.name.HeaderText = "name";
this.name.Name = "name";
this.name.ReadOnly = true;
//
// text
//
this.text.HeaderText = "text";
this.text.Name = "text";
this.text.ReadOnly = true;
//
// remk
//
this.remk.HeaderText = "remk";
this.remk.Name = "remk";
this.remk.ReadOnly = true;
//
// form1BindingSource
//
this.form1BindingSource.DataSource = typeof(TestWindowsForm.Form1);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(747, 382);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).EndInit();
this.ResumeLayout(false); }
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn id;
private System.Windows.Forms.DataGridViewTextBoxColumn name;
private System.Windows.Forms.DataGridViewTextBoxColumn text;
private System.Windows.Forms.DataGridViewTextBoxColumn remk;
private System.Windows.Forms.BindingSource form1BindingSource;如果不设置 dataGridView 的DataGridViewTextBoxColumn ,这个就能绑定数据,设置了 有数据,但是看到的数据内容为空!
找到这个属性设置完成就好了;