Newbie--datagrid understanding

S

SStory

Ok. I got VS2003.
It has the table builder.

What I don't understand is how to bind the datagrid to anything in the IDE.

I placed a SQL connection and SQL command on the form.. I can't get a
datareader to bind it...
Do I have to create a dataset to be able to work with the datagrid (bound)
in the IDE?

I don't always think I need a dataset.

Example, I want to show a datalist, datagrid for Locations that applicant is
willing to work.. This has City, State, Zip.

Need to show it, have ADD button below that brings up a separate form,
meanwhile the datagrid or datalist lists the items with edit button.

Using a dataset with that would seem dumb--each person--different info.

Please give me an idea as to what I should do and if I can use the IDE
features of the datagrid apart from using datasets.

I tried making the datalist before this.. but my items are spaced far apart,
I see no reason in code for that to be.
Also when I click edit it blanks out and nothing happens.

Any help appreciated..

Shane
 
L

Leonard

SStory,

I have gotten the following scheme to work on a pageable
DataGrid, dg, using the Sql flavor of ADO.NET classes. I
hope it helps.

connObject.ConnectionString = "{Your connnect string}";
connObject.Open();
string strText = "SELECT Count(ID) as nbrRows
FROM {your table and where clause}";
// ID is the auto number PK
cmdObject = new SqlCommand(strText, connObject);
cmdObject.CommandType = CommandType.Text;
rdr = cmdObject.ExecuteReader(); // rdr is a SqlDataReader
if (!rdr.HasRows)
throw new {Some exception};
rdr.Read();
int nbrItems = Convert.ToInt32(rdr["nbrRows"]);
rdr.Close(); // Don't forget this!

strText = "SELECT {Your projection};
strText += "FROM {your table & where clause - same as
above}";
cmdObject.CommandText = strText;

rdr = cmdObject.ExecuteReader();
if (!rdr.HasRows)
throw new {some exception};

dg.DataSource = rdr;
dg.VirtualItemCount = nbrItems;
dg.DataBind();
rdr.Close(); // Again, don't forget this

Let me know how this works. Getting the number of items
was necessary because I am using a pageable DataGrid. If
the grid is not pageable, this stuff is not needed.

Enjoy!
 
S

SStory

Thanks for the code, but
what I really want to know is how to bind the datagrid in the Visual Studio
2003 IDE to database data without using a dataset--and thus be able to use
the database property builder.


Leonard said:
SStory,

I have gotten the following scheme to work on a pageable
DataGrid, dg, using the Sql flavor of ADO.NET classes. I
hope it helps.

connObject.ConnectionString = "{Your connnect string}";
connObject.Open();
string strText = "SELECT Count(ID) as nbrRows
FROM {your table and where clause}";
// ID is the auto number PK
cmdObject = new SqlCommand(strText, connObject);
cmdObject.CommandType = CommandType.Text;
rdr = cmdObject.ExecuteReader(); // rdr is a SqlDataReader
if (!rdr.HasRows)
throw new {Some exception};
rdr.Read();
int nbrItems = Convert.ToInt32(rdr["nbrRows"]);
rdr.Close(); // Don't forget this!

strText = "SELECT {Your projection};
strText += "FROM {your table & where clause - same as
above}";
cmdObject.CommandText = strText;

rdr = cmdObject.ExecuteReader();
if (!rdr.HasRows)
throw new {some exception};

dg.DataSource = rdr;
dg.VirtualItemCount = nbrItems;
dg.DataBind();
rdr.Close(); // Again, don't forget this

Let me know how this works. Getting the number of items
was necessary because I am using a pageable DataGrid. If
the grid is not pageable, this stuff is not needed.

Enjoy!
-----Original Message-----
Ok. I got VS2003.
It has the table builder.

What I don't understand is how to bind the datagrid to anything in the IDE.

I placed a SQL connection and SQL command on the form.. I can't get a
datareader to bind it...
Do I have to create a dataset to be able to work with the datagrid (bound)
in the IDE?

I don't always think I need a dataset.

Example, I want to show a datalist, datagrid for Locations that applicant is
willing to work.. This has City, State, Zip.

Need to show it, have ADD button below that brings up a separate form,
meanwhile the datagrid or datalist lists the items with edit button.

Using a dataset with that would seem dumb--each person-- different info.

Please give me an idea as to what I should do and if I can use the IDE
features of the datagrid apart from using datasets.

I tried making the datalist before this.. but my items are spaced far apart,
I see no reason in code for that to be.
Also when I click edit it blanks out and nothing happens.

Any help appreciated..

Shane


.
 

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

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top