• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

DataGrid and HyperLinkColumn

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
udir
Asked:
udir
  • 7
  • 6
1 Solution
 
PHDCommented:
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
 
udirAuthor Commented:
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
 
udirAuthor Commented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
udirAuthor Commented:
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
 
PHDCommented:
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
 
PHDCommented:
Sorry I missed somthing :
   string hl.Text =  DataBinder.Eval(e.Item.DataItem ,"ColumnName");
0
 
PHDCommented:
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
 
udirAuthor Commented:
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
 
PHDCommented:
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
 
PHDCommented:
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
 
udirAuthor Commented:
But //to clear the text automatically bound.
delete all the text, and it is ""
0
 
udirAuthor Commented:
Its working!!!!!!!!!!
Thanks alot (:
0
 
PHDCommented:
You are welkom.
Happy programming :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now