[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

DataGrid Image Column

Posted on 2004-04-09
7
Medium Priority
?
548 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 750 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 750 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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