Datareader, Datagrid and hyperlinks

G

Grant

I am filling a datareader and then assigning the reader to the datagrid as
follows:

SqlDataReader reader = ADOConnect.populateDatareader();
AllResultsDataGrid.DataSource = reader;
AllResultsDataGrid.HeaderStyle.Font.Bold = true;
AllResultsDataGrid.DataBind();

My question is how do I specify that foreach row in the first column - the
text is a hyperlink? This column contains an ID which Id like to have as a
hyperlink so that the user can be directed to another page where they can
edit information for that record. I can do this when binding the datagrid to
a recordset, but Id like to use the datareader instead if possible.

Another question I have with this datagrid is, I want to be able to delete a
row from this page, so could I add an additional column at runtime with a
button or link foreach record - with a funtion attached?

Cheers,
Grant
 
A

Andrew L. Van Slaars

If you have created the datagrid through tags in the designer (assuming VS
is your editor), you can right click on it, choose the "property builder"
option and add a hyperLinkColumn to the grid and move it to the first
position in the list of columns to make it the first column.
Hope this helps
-Andrew
 
A

Andrew L. Van Slaars

I just saw your second question... you could do this using the same steps as
listed in my last response, but instead of the hyperlink column, use the
button column, or you could use a template column if you wanted more control
and options within the cells.

-Andrew
 
G

Grant

Thanks for replying Andrew that did the trick. One more question though, I
have set one of the columns to be a delete colum as in the following code:

ButtonColumn hlcDelete = new ButtonColumn();
hlcDelete.Text = "Delete";

Problem is when the button is created it has no ID or event handler assigned
to it. i need to be able to determine either what row was selected or what
the value of the ID field in that row is so that I can create a SQL query to
delete that record. There are not any samples that i can find - all of them
are done in ASP code, and I prefer not to use the designer to set these
sorts of things. Can this sort of thing be done in c# code when Im binding
the datareader?

Cheers,
Grant
 
G

Grant

No worries I figured it out. I just set the text using the property builder
then assigned an event handler to it. Then get the cell value using

TableCell itemCell = e.Item.Cells[2];
string item = itemCell.Text;

Couldnt be easier.

Cheers,
Grant
 
B

Ben Strackany

You could also check out the ASP.NET quick start pages at

http://samples.gotdotnet.com/quickstart/aspplus/

The "Server-side data access" pages go through the datagrid at a high level.

--
Ben Strackany
www.developmentnow.com


Grant said:
No worries I figured it out. I just set the text using the property builder
then assigned an event handler to it. Then get the cell value using

TableCell itemCell = e.Item.Cells[2];
string item = itemCell.Text;

Couldnt be easier.

Cheers,
Grant

Grant said:
Thanks for replying Andrew that did the trick. One more question though, I
have set one of the columns to be a delete colum as in the following code:

ButtonColumn hlcDelete = new ButtonColumn();
hlcDelete.Text = "Delete";

Problem is when the button is created it has no ID or event handler
assigned to it. i need to be able to determine either what row was
selected or what the value of the ID field in that row is so that I can
create a SQL query to delete that record. There are not any samples that i
can find - all of them are done in ASP code, and I prefer not to use the
designer to set these sorts of things. Can this sort of thing be done in
c# code when Im binding the datareader?

Cheers,
Grant
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top