Solved

DataGrid Image Column

Posted on 2004-04-09
7
542 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Firing an ASP.NET event on selection of a JQuery datepicker 4 46
Dictionary and array of [N] size - performance tuned 12 42
Return array 3 21
C# LINQ 5 27
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 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