Populating a datagrid

G

Guest

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
 
E

Earl

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.
 
G

Guest

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
 
C

Cor Ligthert [MVP]

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
 
G

Guest

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
 
C

Cor Ligthert [MVP]

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
 
G

Guest

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
 
C

Cor Ligthert [MVP]

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top