Solved

DataGrid Image Column

Posted on 2004-04-09
7
539 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fomat Json objects 6 28
Access properties in nested observable collections 8 23
Filename to be appended with DateTimeof Download 3 44
Error in script 11 45
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

20 Experts available now in Live!

Get 1:1 Help Now