PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Populating a datagrid

Reply

Populating a datagrid

 
Thread Tools Rate Thread
Old 07-02-2006, 12:11 AM   #1
=?Utf-8?B?QW50?=
Guest
 
Posts: n/a
Default Populating a datagrid


Hi,
I have a datagrid on a WinForm which I need to poulate with a search result
set.
The search is done with a parameter to a sp so I'm guessing the data grid
can't be bound to a design time Dataset. I have no idea where to start with
this.

I have created a datareader which holds all the returned rows from the
query. How can I load what's in the data reader into the datagrid, or must I
use some other way?

Thank you very much for any suggestions
Ant


  Reply With Quote
Old 07-02-2006, 02:50 AM   #2
Earl
Guest
 
Posts: n/a
Default Re: Populating a datagrid

Well, you aren't so far off. Instead of the datareader, use your sp to
populate a datatable within a dataset and bind that to your datagrid.

"Ant" <Ant@discussions.microsoft.com> wrote in message
news:76827F8B-11D0-4B79-852C-9857BEB04F03@microsoft.com...
> Hi,
> I have a datagrid on a WinForm which I need to poulate with a search
> result
> set.
> The search is done with a parameter to a sp so I'm guessing the data grid
> can't be bound to a design time Dataset. I have no idea where to start
> with
> this.
>
> I have created a datareader which holds all the returned rows from the
> query. How can I load what's in the data reader into the datagrid, or must
> I
> use some other way?
>
> Thank you very much for any suggestions
> Ant
>
>



  Reply With Quote
Old 07-02-2006, 03:42 AM   #3
=?Utf-8?B?QW50?=
Guest
 
Posts: n/a
Default Re: Populating a datagrid

Thanks very much Earl. I'm quite new to ADO.NET.

I know this is wrong, but I don't know how to execute a command against a
datatable:

DataSet dsResults = new DataSet();

DataTable dtResults = dsResults.Tables.Add("resultTable");

sqlConnection1.Open();


// This is not right. How is this done??

dsResults.Tables["resultTable"] = (DataTable)comSearchByName.ExecuteReader();

Thanks for any further input.

Regards
Ant





"Earl" wrote:

> Well, you aren't so far off. Instead of the datareader, use your sp to
> populate a datatable within a dataset and bind that to your datagrid.
>
> "Ant" <Ant@discussions.microsoft.com> wrote in message
> news:76827F8B-11D0-4B79-852C-9857BEB04F03@microsoft.com...
> > Hi,
> > I have a datagrid on a WinForm which I need to poulate with a search
> > result
> > set.
> > The search is done with a parameter to a sp so I'm guessing the data grid
> > can't be bound to a design time Dataset. I have no idea where to start
> > with
> > this.
> >
> > I have created a datareader which holds all the returned rows from the
> > query. How can I load what's in the data reader into the datagrid, or must
> > I
> > use some other way?
> >
> > Thank you very much for any suggestions
> > Ant
> >
> >

>
>
>

  Reply With Quote
Old 07-02-2006, 07:10 AM   #4
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Populating a datagrid

Ant,

Yesterday I have asked you what you where using with an almost the same
question (A webform or a windowsform datagrid).

I wrote that you cannot use for a windowsform datagrid a datareader as
datasource.

You told that you where using a webform, than I gave you a sample for a
datasource and a binding with a sqldatareader.

Now you ask the same however for a windowform with a datareader

What is your goal with asking these answered questions?

Cor


  Reply With Quote
Old 07-02-2006, 07:38 AM   #5
=?Utf-8?B?QW50?=
Guest
 
Posts: n/a
Default Re: Populating a datagrid

Sorry Cor,

I now simply need to know how to load data from a command into a dataset.
Below is what I'm trying to do. Do you have any suggestions on how I would do
this properly?

// I need to return the result set into the resultSet table

dsResults.Tables["resultTable"] = (DataTable)comSearchByName.ExecuteReader();


Thanks for your help
Ant

"Cor Ligthert [MVP]" wrote:

> Ant,
>
> Yesterday I have asked you what you where using with an almost the same
> question (A webform or a windowsform datagrid).
>
> I wrote that you cannot use for a windowsform datagrid a datareader as
> datasource.
>
> You told that you where using a webform, than I gave you a sample for a
> datasource and a binding with a sqldatareader.
>
> Now you ask the same however for a windowform with a datareader
>
> What is your goal with asking these answered questions?
>
> Cor
>
>
>

  Reply With Quote
Old 07-02-2006, 07:54 AM   #6
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Populating a datagrid

Ant,

The datagrid is typical for version 2002/2003 your code is a new part of
2005.

What is it that you are using?

Cor


  Reply With Quote
Old 07-02-2006, 08:40 AM   #7
=?Utf-8?B?QW50?=
Guest
 
Posts: n/a
Default Re: Populating a datagrid

Hi Cor,

Thanks for your interest,

I'm using visual studio 2003. I got the code from MSDN. I think the problem
however is more to do with my lack of knowledge with ADO. I've only started
using it recently & am trying to learn as much as possble. Hence, if you were
using a command object to load data into a table in a data set, how would you
do it. Just a code example would be very helpful.

// What I'm doing
dsResults.Tables["resultTable"] = comSearchByName.ExecuteReader();


// How you do this goes here...

Thanks
Ant


"Cor Ligthert [MVP]" wrote:

> Ant,
>
> The datagrid is typical for version 2002/2003 your code is a new part of
> 2005.
>
> What is it that you are using?
>
> Cor
>
>
>

  Reply With Quote
Old 07-02-2006, 08:50 AM   #8
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Populating a datagrid

An,
\\\

DataTable dt = new DataTable();
SQLConnection conn = new SqlConnection("Server=(Local);" & _
"DataBase=Northwind; Integrated Security=SSPI");
string sqlstr = "SELECT * FROM Employees";
SqlDataAdapter da = New SqlDataAdapter(sqlstr, conn);
da.Fill(dt);
DataGridView1.DataSource = dt;
///

I hope this helps,

cor


  Reply With Quote
Old 07-02-2006, 09:28 AM   #9
=?Utf-8?B?QW50?=
Guest
 
Posts: n/a
Default Re: Populating a datagrid

Hi Cor,

Thanks greatly for your help.
I'm using a sp. I tried the sp instead of the string that you suppled in the
dataAdapter like so:

SqlDataAdapter da = new SqlDataAdapter(comSearchByName,sqlConnection1);

but received an error:
Cannot convert from System.Data.SqlClient.SQLCommand to string

How do I get around this problem?


Thanks very mucvh for your help on this
Ant




"Cor Ligthert [MVP]" wrote:

> An,
> \\\
>
> DataTable dt = new DataTable();
> SQLConnection conn = new SqlConnection("Server=(Local);" & _
> "DataBase=Northwind; Integrated Security=SSPI");
> string sqlstr = "SELECT * FROM Employees";
> SqlDataAdapter da = New SqlDataAdapter(sqlstr, conn);
> da.Fill(dt);
> DataGridView1.DataSource = dt;
> ///
>
> I hope this helps,
>
> cor
>
>
>

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off