T
Tony Johansson
Hello!
I have a complete program listed below that use the northwind database table
Customers.
I have one control in the form and that is a DataGridView called
dataGridViewCustomer.
I use a SqlDataAdapter that pass the sql statement Select * from customers
in the c-tor.
When I run in the debugger I can see that the DataSet thisDataSet contains
all the rows that should be displayed
in the DataGridView but nothing is being displayed.
As you can see I set the DataSource for the DataGridView to the DataSet
thisDataSet.
I event to a refresh() on the DataGridView but nothing helps.
So does anyone know why my DataGridView doesn't display the contents of the
DataSet ?
//Here is the program
using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
DataSet thisDataSet = new DataSet();
SqlDataAdapter custAdapter = new SqlDataAdapter
("Select * from customers", thisConnection);
custAdapter.Fill(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = thisDataSet;
this.dataGridViewCustomer.Refresh();
}
}
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGridViewCustomer = new
System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomer)).BeginInit();
this.SuspendLayout();
this.dataGridViewCustomer.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewCustomer.Location = new System.Drawing.Point(25,
24);
this.dataGridViewCustomer.Name = "dataGridViewCustomer";
this.dataGridViewCustomer.Size = new System.Drawing.Size(506, 342);
this.dataGridViewCustomer.TabIndex = 0;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(543, 408);
this.Controls.Add(this.dataGridViewCustomer);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomer)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridViewCustomer;
}
//Tony
I have a complete program listed below that use the northwind database table
Customers.
I have one control in the form and that is a DataGridView called
dataGridViewCustomer.
I use a SqlDataAdapter that pass the sql statement Select * from customers
in the c-tor.
When I run in the debugger I can see that the DataSet thisDataSet contains
all the rows that should be displayed
in the DataGridView but nothing is being displayed.
As you can see I set the DataSource for the DataGridView to the DataSet
thisDataSet.
I event to a refresh() on the DataGridView but nothing helps.
So does anyone know why my DataGridView doesn't display the contents of the
DataSet ?
//Here is the program
using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
DataSet thisDataSet = new DataSet();
SqlDataAdapter custAdapter = new SqlDataAdapter
("Select * from customers", thisConnection);
custAdapter.Fill(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = thisDataSet;
this.dataGridViewCustomer.Refresh();
}
}
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGridViewCustomer = new
System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomer)).BeginInit();
this.SuspendLayout();
this.dataGridViewCustomer.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewCustomer.Location = new System.Drawing.Point(25,
24);
this.dataGridViewCustomer.Name = "dataGridViewCustomer";
this.dataGridViewCustomer.Size = new System.Drawing.Size(506, 342);
this.dataGridViewCustomer.TabIndex = 0;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(543, 408);
this.Controls.Add(this.dataGridViewCustomer);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomer)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridViewCustomer;
}
//Tony