Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DataGrid and HyperLinkColumn

Posted on 2007-07-30
13
Medium Priority
?
555 Views
Last Modified: 2011-09-20
Hi,
Is it possible to bind data to DataGrid (the hole table - dataGrid.AutoGenerateColumns = true;)
and turn the first column into - HyperLinkColumn? (each value in the column must be from that type, not just the header, or maybe it is a link lable?).
0
Comment
Question by:udir
  • 7
  • 6
13 Comments
 
LVL 6

Accepted Solution

by:
PHD earned 2000 total points
ID: 19598311
Yes you have to do it in the Itemdatabound event :

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
         if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
                        HyperLink hl = new HyperLink();
                        hl.Text = "click me";
                        hl.NavigateUrl = "mypage.aspx";
                        e.Item.Cells[0].Controls.Add(hl);
                  }
0
 
LVL 1

Author Comment

by:udir
ID: 19598408
Hi,
Thanks for the reply,
OK thats looks good...
One thing :
I add the event On RunTime, and tried to add also :
this.dataGrid.ItemDataBound += new DataGridSortCommandEventHandler(dataGrid_ItemDataBound);
but got an error :
No overload for 'dataGrid_ItemDataBound' matches delegate 'System.Web.UI.WebControls.DataGridSortCommandEventHandler'

Any Idea?
0
 
LVL 1

Author Comment

by:udir
ID: 19598439
Oh sorry,
if i change to :
this.dataGrid.ItemDataBound += new DataGridSortCommandEventArgs(dataGrid_ItemDataBound);
i get an error :
No overload for method 'DataGridSortCommandEventArgs' takes '1' arguments

i need to add an object - commandSource + DataGridSortCommandEventArgs - dce
I'm not sure what to put in those 2 parameters.
Any idea?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:udir
ID: 19598587
Oh sorry again (:
I put  :
this.dataGrid.ItemDataBound += new DataGridItemEventHandler(DataGrid_ItemDataBound);
and its fine.
But therare 2 problems :
1) I think it need to be HyperLinkColumn becuase it has to be not just "click me", it has to get the values
from the original column, and in HyperLinkColumn  i can add : hl.DataNavigateUrlField....
but then i can't write :  e.Item.Cells[0].Controls.Add(hl);
2) i turn the dataGrid to Sortable and only after i click on the sort command (at the Header) then i can see the Link Lables.

Any idea?
Thanks
0
 
LVL 6

Expert Comment

by:PHD
ID: 19598630
1) you can get the value with :
      DataBinder.Eval(e.Item.DataItem ,"ColumnName"); where columnname id the name of the column in the dataset
      or you can try this statement : string hl.Text = e.Item.Cells[0].Text;
   
0
 
LVL 6

Expert Comment

by:PHD
ID: 19598634
Sorry I missed somthing :
   string hl.Text =  DataBinder.Eval(e.Item.DataItem ,"ColumnName");
0
 
LVL 6

Expert Comment

by:PHD
ID: 19598640
2) I'm not sure to understands what you sayed but are sure that you bind the datagrid at the first load of the page ?
0
 
LVL 1

Author Comment

by:udir
ID: 19598694
About 1 :
do u mean -
           HyperLink hl = new HyperLink();
            hl.Text =  (string)DataBinder.Eval(e.Item.DataItem ,"UserId");
            hl.NavigateUrl = "mypage.aspx";
            e.Item.Cells[0].Controls.Add(hl);
----------------------------------------------------
First, why i get in each cell the value from "UserId" twice?
second, those links are not soppose to go to another aspx, i just need to get the value from the linkLable, after i click it (and if i take off the line - hl.NavigateUrl = "mypage.aspx";
then it is not a linkLables anymore.
(Sorry for beeing a little anoying (:

0
 
LVL 6

Expert Comment

by:PHD
ID: 19598714
First  :
           HyperLink hl = new HyperLink();
            hl.Text =  (string)DataBinder.Eval(e.Item.DataItem ,"UserId");
            hl.NavigateUrl = "mypage.aspx";
            e.Item.Cells[0].Controls.Add(hl);
            e.Item.Cells[0].Text = "";  //to clear the text automatically bound.

 
0
 
LVL 6

Expert Comment

by:PHD
ID: 19598720
If don't want to make a redirection to another page but just catch the value of the userid try to use an linkbutton instead of a hyperlink
0
 
LVL 1

Author Comment

by:udir
ID: 19598766
But //to clear the text automatically bound.
delete all the text, and it is ""
0
 
LVL 1

Author Comment

by:udir
ID: 19598893
Its working!!!!!!!!!!
Thanks alot (:
0
 
LVL 6

Expert Comment

by:PHD
ID: 19598948
You are welkom.
Happy programming :-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question