{"id":2105,"date":"2006-06-07T14:20:53","date_gmt":"2006-06-07T21:20:53","guid":{"rendered":"http:\/\/hi-alex.com\/?p=2105"},"modified":"2006-06-07T14:20:53","modified_gmt":"2006-06-07T21:20:53","slug":"bar-graph-column-in-a-sorted-datagrid","status":"publish","type":"post","link":"http:\/\/www.hi-alex.com\/?p=2105","title":{"rendered":"Bar Graph column in a sorted datagrid"},"content":{"rendered":"<p>[align=center]<img decoding=\"async\" src=\"wp-content\/uploads\/2011\/200606070926516478.gif\" \/>[\/align]<br \/>\n[b][color=Orange]Introduction[\/color][\/b]<br \/>\nHi, this is my first attempt at an article for CodeProject. I&#8217;ve used enough of other peoples so thought I better give something back. I&#8217;ve been developing websites for the last 6 years or so doing everything from PHP to C#.NET. For other examples please visit the development section on http:\/\/www.chemlock.co.uk\/. The project below is ASP.NET in C#. There was a need to display a simple bar per row in the datagrid to quickly illustrate an amount. This is very simple so I&#8217;ve set it to beginner level. Any questions please mail me.<br \/>\n[b]Graph display in datagrids[\/b]<br \/>\nThis morning I had the pleasure of re-writing a consultants code into something which worked and was open to development. For some strange reason this guy wrote all his code in classic ASP hard coding the database connection strings into a file and creating all the SQL on the fly. This was slow. Not only because it was Classic ASP and the SQL strings were constantly changing so the server couldn\u2019t cache the query but because it was badly written. One page got passed two variables in the http request stream and then used those variables to pull the same two values back from the database. Pointless and time consuming. Anyway the results where thrown into tables and one column on all the tables was a graph to show the amount of time booked to a project per week. This was out of a maximum of 60 hours (Don\u2019t know which fool is doing those hours). My friend the highly paid consultant had done this by making a table within a table cell with a width equal to the amount of hours. This meant that if you have booked 34 hours to a project the graph would be 34 pixels long.<br \/>\nI was asked to fix some of the SQL as the results where not very relevant for one of the reports but thought it would be a fairly quick and simple job to simply move the lot to .Net and Stored Procs. So here is my quick and dirty guide to creating a graph column in a datagrid. It\u2019s all nice and basic(C# actually), certainly not rocket science.<br \/>\nFirst of all Create a nice clean WebForm Here is the code behind.<br \/>\n[code=c#]private void Page_Load(object sender, System.EventArgs e)<br \/>\n{<br \/>\nif(!IsPostBack)<br \/>\n{<br \/>\nif(Request[\u201dEmployeeNo\u201d] != null)<br \/>\n{<br \/>\nDb db = new Db();<br \/>\nArrayList parameters = new ArrayList();<br \/>\nparameters.Add(new DbParameter(\u201d@EmployeeNo\u201d,DbType.String,10,\u201dEmployeeNo\u201d,Request[\u201dEmployeeNo\u201d].ToString()));<br \/>\nSession[\u201ddataset\u201d] = db.GetDataSet(\u201dGetEmployeeBookings\u201d,parameters);<br \/>\nSession[\u201dSortOn\u201d] = \u201cEndDate\u201d;<br \/>\nSession[\u201dSortDirection\u201d] = \u201d DESC\u201d;<br \/>\n}<br \/>\n}<br \/>\nBindGrid();<br \/>\n} [\/code]<br \/>\nOk so here notice that I\u2019m using a class called DB to get at my database and create a dataset. Use what you like here the main point is to end up with a dataset of your results. I also set up initial Sort Parameters for my datagrid and the initial direction they will be sorted in. Every post back gets the grid bound with data but only the initial pass gets the data from the Database. Obviously if this data was likely to be changed as a result of any page action I would move this database call to a seperate methods but as it\u2019s a static report and I\u2019m in a hurry I\u2019ve left it as I initially did it.<br \/>\n[code=c#]private void BindGrid()<br \/>\n{<br \/>\nif (Session[\u201ddataset\u201d] != null)<br \/>\n{<br \/>\nDataTable dt = ((DataSet)Session[\u201ddataset\u201d]).Tables[0];<br \/>\nDataView dv = dt.DefaultView;<br \/>\ndv.Sort = Session[\u201dSortOn\u201d].ToString() + Session[\u201dSortDirection\u201d].ToString();<br \/>\nthis.DataGrid1.DataSource = dv;<br \/>\nthis.DataGrid1.DataBind();<br \/>\n} [\/code]<br \/>\nHere is the BindGrid method. It first checks that something exists in the Session variable. If it does it extracts it creates a dataview sorted correctly and then binds that to our DataGrid control. Notice how very lazy I\u2019ve been with my DataGrid naming. Remeber this was quick and dirty.<br \/>\n[code=c#]private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)<br \/>\n{<br \/>\nSession[\u201dSortOn\u201d] = e.SortExpression.ToString();<br \/>\nif (Session[\u201dSortDirection\u201d] == null || Session[\u201dSortDirection\u201d].ToString() == \u201d ASC\u201d)<br \/>\n{<br \/>\nSession[\u201dSortDirection\u201d] = \u201d DESC\u201d;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nSession[\u201dSortDirection\u201d] = \u201d ASC\u201d;<br \/>\n}<br \/>\nthis.DataGrid1.CurrentPageIndex = 0;<br \/>\nBindGrid();<br \/>\n} [\/code]<br \/>\nHere is a nice and standard Sort Event Method. Basically if you select a column heading that has already been selected it will change the direction of that column. Any other column and the grid gets sorted on the new column heading.<br \/>\n[code=c#]private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)<br \/>\nif(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)<br \/>\n{<br \/>\ndecimal hours = decimal.Parse(e.Item.Cells[3].Text);<br \/>\ndecimal percentage = (100m \/ 60m) * hours;<br \/>\ne.Item.Cells[4].Text = \u201c<\/p>\n<div style= \u201cWIDTH: 100%\u201d>\n<div style= \u2018BACKGROUND-COLOR:RED;HEIGHT: 10px;WIDTH:\u201d+ percentage + \u201c%\u2019><\/div>\n<\/div>\n<p> \u201c;}<br \/>\n} [\/code]<br \/>\nThis is where the graph is built. In my on-going quest to get shot of tables where tables shouldn\u2019t be I\u2019ve gone for a &#8220;DIV&#8221; approach. Firstly I create a div that fills the entire cell. Then inside this I create a div that takes up a percentage of the parent div. Please note that without the height command the graph will not show in Firefox. So I\u2019ve got my 60 hours and then work out a percentage of this for the width of the child div. The code is set to apply this when each line of the datagrid gets databound. Each time the hours is worked out from another column in the database (In this instance column 3) and Parsed to a decimal. The resultant html code is injected into the cell 4. All this is set to only occur when the datagrid item (or row) is an Item or Alternating item type, after all we don\u2019t want to create a graph for the header or pager section.<br \/>\nSo there we have it very quick and very dirty but it does the trick.<br \/>\nAs I said at the start fairly simple, if my description has complicated things then please let me know. Other examples on www.chemlock.co.uk<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[align=center][\/align] [b][color=Orange]Introduction[\/color][\/b] Hi, this is my first attempt at an article for CodeProject. I&#8217;ve used enough of other peoples so thought I better give something back. I&#8217;ve been developing websites for the last 6 years or so doing everything from PHP to C#.NET. For other examples please visit the development section on http:\/\/www.chemlock.co.uk\/. The project [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2105","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"_links":{"self":[{"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=\/wp\/v2\/posts\/2105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2105"}],"version-history":[{"count":0,"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=\/wp\/v2\/posts\/2105\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2105"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hi-alex.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}