Display data on DataGridView

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

I'll need to display data in a grid on a Windows Form, call it
"frmGrid". Since I cannot find "GridView" in the Toolbox, I pick
"DataGridView" instead (call it "dataGridView1"). I have the code
below:

mConnection = new SqlConnection("Data
Source=Westwind;Initial Catalog=db_dynamic_trading;Integrated
Security=True;");
mConnection.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter
("db_dynamic_trading.dbo.emily_get_records", mConnection);
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
da.Fill(ds);

frmGrid fGrid = new frmGrid(da); // Call constructor
below to connect dataGridView1 with da

fGrid.Show();

In constructor for frmGrid, I have the code below to connect
dataGridView1 with da:


public frmGrid(SqlDataAdapter da)
{
InitializeComponent();

this.dataGridView1.DataSource = da;
this.dataGridView1.DataBind();
}

Now the code cannot even compile because it complains that
'System.Windows.Forms.DataGridView' does not contain a definition for
'DataBind'.

Any advice on how to get this fixed?
 
Curious said:
I'll need to display data in a grid on a Windows Form, call it
"frmGrid". Since I cannot find "GridView" in the Toolbox, I pick
"DataGridView" instead (call it "dataGridView1"). I have the code
below:

mConnection = new SqlConnection("Data
Source=Westwind;Initial Catalog=db_dynamic_trading;Integrated
Security=True;");
mConnection.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter
("db_dynamic_trading.dbo.emily_get_records", mConnection);
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
da.Fill(ds);

frmGrid fGrid = new frmGrid(da); // Call constructor
below to connect dataGridView1 with da

fGrid.Show();

In constructor for frmGrid, I have the code below to connect
dataGridView1 with da:


public frmGrid(SqlDataAdapter da)
{
InitializeComponent();

this.dataGridView1.DataSource = da;
this.dataGridView1.DataBind();
}

Now the code cannot even compile because it complains that
'System.Windows.Forms.DataGridView' does not contain a definition for
'DataBind'.

Any advice on how to get this fixed?

It appears that you have some basic misunderstandings about how ADO.NET
works with a WinForm(s) application.

If you use MSDN (msdn.microsoft.com) to check available methods for the
DataGridView class, you'll see that there is no DataBind method.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview_members(VS.80).aspx

Further, if you search MSDN for "DataBind method", you'll see only ASP.NET
references.

To discover the solution you want, you can either find tutorials:

http://social.msdn.microsoft.com/Search/en-US?query=ado.net tutorial msdn&ac=1

Or, you can create a simple data project using VS' automatic facilities and
then observe the generated code. Simply defining a data source and dragging
it to an empty form should do what you want.
 
Back
Top