Solved

DataGrid Image Column

Posted on 2004-04-09
7
538 Views
Last Modified: 2010-04-15
I need to list out a DataGrid with 2 columns (name, actions).  The name and id are bound from a HashTable (this returns my name and id associated with the name) but I need the actions column to simply be a set of 2 images that represent actions of edit and delete (you know something like a notepage with a pencil for edit and a red x for delete) with an html link (using the id that I get when filling the DataGrid, something like /edit.aspx?id=X).  The problem is with this actions column.  There is so much info for the DataGrid column that I can't seem to narrow this down.  I think I'm on the right track when I think I need a TemplateColumn but I'm fully confused ... any help?

Thanks for your time and help!
0
Comment
Question by:rkekos
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 10795915
Read this article:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_CustDataGrid.asp

It shows how to write your own DataGrid column class derived from DataGridTextBoxColumn or DataGridBoolColumn and override it's Paint function. You need to apply this solution changing drawing code in Paint.
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 10803521
Hi,
Are you talking about Windows application or Web application??

Regards,
venki
0
 
LVL 12

Assisted Solution

by:dfiala13
dfiala13 earned 250 total points
ID: 10803694
It's pretty easy. Just add two hyperlinks to a template column.  Set the hyperlink navigation in the ItemDataBound event of the grid.

HTML for the grid. Set your ImageUrls as needed...


<asp:DataGrid  runat="server" AutoGenerateColumns="False">
      <Columns>
            <asp:BoundColumn DataField="Key" HeaderText="Name"></asp:BoundColumn>
            <asp:TemplateColumn HeaderText="Action">
                  <ItemTemplate>
                        <asp:HyperLink id="hlEdit"  ImageUrl="Edit.gif" runat="server"></asp:HyperLink>
                        <asp:HyperLink id="hlDelete" ImageUrl="Delete.gif" runat="server"></asp:HyperLink>
                  </ItemTemplate>
            </asp:TemplateColumn>
      </Columns>
</asp:DataGrid>


Code-behind:

//declare grid
protected System.Web.UI.WebControls.DataGrid dg;

//load the grid
private void Page_Load(object sender, System.EventArgs e)
{
      if(!Page.IsPostBack)
      {
            Hashtable ht = new Hashtable();
            ht.Add("My Form", 1);
            ht.Add("Your Form", 2);
            dg.DataSource = ht;
            dg.DataKeyField = "Key";
            dg.DataBind();
      }

}

//this gets called when the grid is bound
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
            DictionaryEntry en = (DictionaryEntry )e.Item.DataItem;
            
            HyperLink hlEdit  = (HyperLink)e.Item.FindControl("hlEdit");
            HyperLink hlDelete  = (HyperLink)e.Item.FindControl("hlDelete");
            hlEdit.NavigateUrl = "edit.aspx?ID="  + en.Value;
            hlDelete.NavigateUrl = "delete.aspx?ID="  + en.Value;
            

      }
}
0
 

Author Comment

by:rkekos
ID: 10803845
WOW!!!

Very nice code sample dfiala13.  During the time I posted my question and then trying to continue with my problem I ended up using just a table.  I guess now my main question would be why in the world would I use a DataGrid apposed to a Table control.  Remember I just need to display data with a few functions, nothing is actually tied to the database directly meaning that I'm not using a DG to edit data on the fly.  I'm using "edit" as in the term of business rules (ie, edit location of IIS, DNS, folder then the actual database).  For the sake of quick programming a Table control seems to be faster for me but DANG dfiala13 you fully wrote everything I was talking about.  Nice job ... you deserve way more then the 500 points!
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10805202
Thank you.

If you are not editing the grid, then a table is probably just as easy to use as a DataGrid, though with databinding and autogenerated columns, it is sure easy to populate a grid with a few lines of code. (I frequently use XML/XSLT for creating read-only tables). The .NET framework handles a lot of the grunt work of putting everything together and make sure it is in the proper place when using the grid.  As soon as you want to edit or sort, then it is much easier to use the grid to get things set up and to access them once you post back.

Have fun.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now