Solved

DataGrid Image Column

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

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